2

What is the most effective way to modify deep value in multidimensional array?

I have following json:

[
  {"name":"Series 1","data":[
    {"x":1506470700,"y":null},
    {"x":1506499200,"y":null},
    {"x":1506499500,"y":483981},
    {"x":1506499800,"y":504588},
    {"x":1506500100,"y":502926},
    {"x":1506500400,"y":501161},
    {"x":1506500700,"y":506453}]
  },{"name":"Series 2","data":[
    {"x":1506470700,"y":null},
    {"x":1506499200,"y":null},
    {"x":1506499500,"y":-490671},
    {"x":1506499800,"y":-495593},
    {"x":1506500100,"y":-512765},
    {"x":1506500400,"y":-479475},
    {"x":1506500700,"y":-531689}]
  }
]

I'd like to multiply x values by 1000. I can process it using this code for example:

arrayFromJson.forEach((series) => {
    series.data.forEach((dataSet) => {
        dataSet.x *= 1000;
    });
});

But I was wondering whether there isn't more effective/elegant way to do it.

2
  • 1
    "better" is highly subjective. I don't see anything wrong with your current solution. Commented Sep 27, 2017 at 8:54
  • Modifed the adjective. Thanks Commented Sep 27, 2017 at 10:04

4 Answers 4

2

Use array map:

var arr = [{"name":"Series 1","data":[{"x":1506470700,"y":null},{"x":1506499200,"y":null},{"x":1506499500,"y":483981},{"x":1506499800,"y":504588},{"x":1506500100,"y":502926},{"x":1506500400,"y":501161},{"x":1506500700,"y":506453}]},{"name":"Series 2","data":[{"x":1506470700,"y":null},{"x":1506499200,"y":null},{"x":1506499500,"y":-490671},{"x":1506499800,"y":-495593},{"x":1506500100,"y":-512765},{"x":1506500400,"y":-479475},{"x":1506500700,"y":-531689}]}];

arr.map(it => {
    it.data = it.data.map(dt => { dt.x = dt.x * 1000 ; return dt;})
});

console.log(arr);

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

1 Comment

The OP's forEach doesn't generate new arrays, a map here is an overhead that's not required.
1

Your solution is totally fine, but keep in mind that you're mutating the original array, which could potentially cause some side-effects.

For an immutable solution use Array.prototype.map and Object.assign:

var data = [{"name":"Series 1","data":[{"x":1506470700,"y":null},{"x":1506499200,"y":null},{"x":1506499500,"y":483981}]}];

var changedData = data.map(series => {
    return Object.assign({}, series, {
      data: series.data.map(dataSet => { 
        return Object.assign({}, dataSet, { x: dataSet.x * 1000 });
      })
    });
});

console.log(data);
console.log(changedData);

1 Comment

Why to use Object.assign instead of =?
0

You could write it a bit shorter, without some parentheses and curly brackets. But you need still nested iterations.

arrayFromJson.forEach(series => series.data.forEach(dataSet => dataSet.x *= 1000));

Comments

0

premising that your current code is perfectly ok, on es6, a ( possibly faster and more readable ) variant could be

for( let series of arrayFromJson )
  for( let data of series.data )
    data.x *= 1000;

on es<6, 'for in' will work too ( given that you don't need ordered traversal, but provided those arrays have no extra own prop ... )

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.