Reputation: 861
Suppose I have an array of object as:
const bookDetails = [{"author":"john","email":"[email protected]","readingTime":"121"},
{"author":"romero","email":"[email protected]","readingTime":908},
{"author":"romero","email":"[email protected]","readingTime":1212},
{"author":"buck","email":"[email protected]","readingTime":1902},
{"author":"buck","email":"[email protected]","readingTime":12125},
{"author":"romero","email":"[email protected]","readingTime":500},
{"author":"john","email":"[email protected]","readingTime":10},
{"author":"legend","email":"[email protected]","readingTime":12}
{"author":"john","email":"[email protected]","readingTime":1890}]
I have an object as: const toMatch = {"[email protected]":1212,"[email protected]":1248,"[email protected]":909}
I want to replace emailId with corresponding author.
So my expected O/P should be: {"romero":1212,"john":1248,"buck":909}
If anyone needs any further information please let me know.
Upvotes: 1
Views: 88
Reputation: 879
You can generate a new object by iterating over bookDetails
and look up the values from toMatch
Like this
const bookDetails=[{"author":"john","email":"[email protected]","readingTime":"121"},{"author":"romero","email":"[email protected]","readingTime":908},{"author":"romero","email":"[email protected]","readingTime":1212},{"author":"buck","email":"[email protected]","readingTime":1902},{"author":"buck","email":"[email protected]","readingTime":12125},{"author":"romero","email":"[email protected]","readingTime":500},{"author":"john","email":"[email protected]","readingTime":10},{"author":"legend","email":"[email protected]","readingTime":12},{"author":"john","email":"[email protected]","readingTime":1890}]
const toMatch = {"[email protected]":1212,"[email protected]":1248,"[email protected]":909}
const result = {};
for (const detail of bookDetails) {
const {email, author} = detail;
if (email in toMatch) {
result[author] = toMatch[email];
}
}
console.log(result);
Upvotes: 2
Reputation: 300
There you go, I'm 100% you can do this easier, but this is the fastest response that came to my mind:
function changeData(bookDetails){
let returnableObject = {};
bookDetails.forEach(book => {
returnableObject[book.author] = book.readingTime
})
return returnableObject;
}
*EDIT -> This would work if your Author name IS UNIQUE if the author name is not unique you should change the name or maybe use an array of objects, not an object with multiple values...
*EDIT 2 -> I think I didnt understand your question.
Upvotes: 2
Reputation: 4337
Ah, you want to matching names from emails based on the array of objects. Is that right? If so..
const bookDetails=[{"author":"john","email":"[email protected]","readingTime":"121"},{"author":"romero","email":"[email protected]","readingTime":908},{"author":"romero","email":"[email protected]","readingTime":1212},{"author":"buck","email":"[email protected]","readingTime":1902},{"author":"buck","email":"[email protected]","readingTime":12125},{"author":"romero","email":"[email protected]","readingTime":500},{"author":"john","email":"[email protected]","readingTime":10},{"author":"legend","email":"[email protected]","readingTime":12},{"author":"john","email":"[email protected]","readingTime":1890}]
function namesMap(object,list){
//to save complexity(assuming the array is huge), I'd just loop through the list once to get the data I need
var obj={} //relates emails to names
list.forEach(item=>{
obj[item.email]=item.author
})
var toReturn={}
Object.keys(object).forEach(email=>{
toReturn[obj[email]]=object[email] //email to name conversion
})
return toReturn
}
//example usage
const toMatch = {"[email protected]":1212,"[email protected]":1248,"[email protected]":909}
console.log(namesMap(toMatch,bookDetails))
Upvotes: 2