0

I have the data like

const data = [
    {}

when i called the function updateScore like this

const result = update

i want to return the new object that add the value in array

const expected = [

        ];

How can i using the lodash for add object to array data

1 Answer 1

2

The desired result is reachable with pure JS. Possible solution:

const data = [
    {
        subject: 'math',
        students: [{ name: 'luffy', score: 10 }, { name: 'zoro', score: 15 }]
    },
    {
        subject: 'science',
        students: [{ name: 'luffy', score: 15 }, { name: 'zoro', score: 25 }]
    }
];

const obj = { 
    name: 'sanji',
    scores: {
      math: 22,
      science: 33
    }
}

const updateStudentScore = ({ name, scores }) => {
  Object.keys(scores).forEach(v => {
    data.find(c => c.subject == v).students
        .push({ name: name, score: scores[v] })
  });
  return data;
}

console.log(updateStudentScore(obj));

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

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.