Home > OS >  How to add values ​inside a large array that consists of many objects with maintaining the previous
How to add values ​inside a large array that consists of many objects with maintaining the previous

Time:09-08

Created https://codepen.io/Spawnes/pen/LYmVMJj in which place a large array of data. The task description will be duplicated here:

You need to add the values ​​of X with all subsequent X

You need to add Y values ​​with all subsequent Y

You need to add marker.color values ​​followed by all marker.color

To end up with:

1) 1 frame unchanged
2) On the second frame, everything that was in the first   data from the second.
3) On the third frame, the sum of 1 and 2   new data from the third frame.
4) On the fourth frame, the previous result   data from frame 4

And so on

I'll add another screenshot demonstrating the data itself: screenshot

As you can see, there are quite a lot of objects inside. And you just need to make an addition while maintaining the previous calculations.

1   2 = 3. 

3   2 = 5

5   6 = 11

11   2 = 13

CodePudding user response:

let firstArr = [1, 5, 10]
let secondArr = [5, 4, 12]
let thirdArr = [8, 9, 1]

const calc = (...args) => {
  let store = []
  return args.map((arr, z) => {
    let newArr =  arr.map((num, i) => {
      if(z > 0){
        return (store[i]   num)
       }
       return num
    })
    
    store = newArr
    return newArr
  })
}

console.log(calc(firstArr, secondArr, thirdArr))
Ok taking this as a starting point: Result [1, 5, 10] [6, 9, 22] [14, 18, 23]

The above function does the trick, you have to obviously change it a bit for ur use case specifically, but in general this is what u are looking for.

CodePudding user response:

Given the complexity of your question this is NOT an answer but a clarification to help you write your question in simpler terms:

So to clarify?

UPDATED:

You want to convert

myArray = [
    {
        "data": [
            {
                "x": [46,38,71, ....],
                "y": [1,2,3, ....],
            }, {
                "x": [10, 10, 20, ....],
                "y": [100, 500, ....],
            },
            {
                "x": [1, 1, 0, ....],
                "y": [10, 10 ....],

to

myArray = [
    {
        "data": [
            {
                "x": [46,38,71, ....],
                "y": [1,2,3, ....],
            }, {
                "x": [56, 48, 91, ....], // updated (ie 46   10 = 56)
                "y": [101, 502, 3 ....], // updated
             }, {
                "x": [57, 49, 91, ....], // updated (ie 56   1 = 57)
                "y": [111, 512, 3 ....], // updated

My attempted answer based on that

const convertData = obj => {
  const prev = {
    x: [],
    y: []
  }
  return obj.data.map(item => {
    const newX = item.x.map((val, ind) => (
      prev.x[ind] = val   (prev.x[ind] ?? 0)
    ))
    const newY = item.y.map((val, ind) => (
      prev.y[ind] = val   (prev.y[ind] ?? 0)
    ))
    // add other stuff to x or y here accordingly...
    return {x: newX, y: newY}
    
  })
}

const inc = myA => myA.map(obj => convertData(obj))
const myArray = [{
      "data": [
            {
                "x": [46,38,71],
                "y": [1,2,3]
            }, {
                "x": [10, 10, 20],
                "y": [100, 500]
            },
            {
                "x": [1, 1, 0],
                "y": [10, 10]
             }
      ]
 }]
 
 console.log(inc(myArray))

  • Related