0

I have a JSON array that represents a list of objects (people). every object (every person) has name attribute, image, and an array of numbers.

Example:

"people":[  
   {  
      "name":"nunu",
      "image":"image1",
      "numbers":{  
         "total":50,
         "vector":[  
            10,
            20,
            5,
            10,
            5
         ]
      }
   }
];

My goal is to update all vectors and append some calculation to each vector.

This is what I tried:

this.people = this.people.map((person) => {
      return person.numbers.vector.map((num) => {
        return Math.round(((num / divider) * 100) / 100);
      });
    });

The problem is that people is being replaced by the numbers in my vector and I lose the people data.

How I can update the vectors without making nay change to any other data?

2
  • Use .forEach() on the outer array, and just update person.numbers.vector for each one. Commented Oct 11, 2017 at 14:43
  • 3
    You're misusing .map Commented Oct 11, 2017 at 14:43

4 Answers 4

1

Due to .map() specification it creates a new array, to process top-level list use .forEach() instead:

this.people.forEach(person => 
  person.numbers.vector = person.numbers.vector.map(num =>
    Math.round(((num / divider) * 100) / 100)
  );
);
Sign up to request clarification or add additional context in comments.

Comments

0
people = people.map((person) => {
    person.numbers.vector =  person.numbers.vector.map((num) => {
        return Math.round(((num / divider) * 100) / 100);
    });
    return person;
});

You were returning the vector as the value for person, you need to change the value of vector then return person instead.

Comments

0

Try using spread operator to update person object and save all data. For example, calculate total value as sum of nums:

 this.people = this.people.map((person) => {
   let total = person.numbers.vector.reduce((prevNum, nextNum) => {
     return prevNum + nextNum;
   });

   return {
      ...person,
      numbers: {
         ...person.numbers,
         total
      }

  }
});

At the same way you can change vector values, for example

Comments

0

If you use the new spread operator and something like Babel this becomes trivial:

const source = {
  "people": [{
    "name": "nunu",
    "image": "image1",
    "numbers": {
      "total": 50,
      "vector": [
        10,
        20,
        5,
        10,
        5
      ]
    }
  }]
};

const newSource = {
  ...source,
  people: source.people.map(person => {
    return {
      ...person,
      numbers: {
        ...person.numbers,
        vector: person.numbers.vector.map(n => Math.round(((n / 2) * 100) / 100))
      }
    }
  })
};

Here is more on the spread operator.

As a side note, using the spread operator creates a new object and using map creates a new array. This way you will always have a new object and can't change the old one. Using const with this type of code is also a good practice.

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.