1

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.

1
  • Ah, you want to matching names from emails based on the array of objects. Is that right? If so I have an answer? Commented May 27, 2021 at 12:23

3 Answers 3

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

This was really helpful. Thank you. :)
2

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))

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.