Home > Software design >  How do you combine two like arrays of objects into a single array object?
How do you combine two like arrays of objects into a single array object?

Time:11-22

I'm sorry if I'm asking a question that has been asked before, I had a hard time wording this one.

Basically, my API is returning an array of objects that are alike

const response.survey = [
  {
    1: { id: 1, user: user_1, points: 5 },
    2: { id: 2, user: user_2, points: 3 }...
  }
],
[
  {
    1: { id: 1, user: user_1, points: 10 },
    2: { id: 2, user: user_2, points: 0 }...
  }
],...

This can carry on for hundreds of arrays potentially, depending on users submissions through our forms. I'm stuck on how to combine them into something like:

[
  { user: user_1, points: 15 },
  { user: user_2, points: 3 },...
]

Note that the data that returned from the api has a key for the object, like Array[Object[1]], Array[Object[2]], etc.

In my current development environment, I'm returning 2x sets of 25 data points, so I would expect my final array of objects to be indexed at 25, but right now I'm getting 52. I believe there is a shorter way to handle this, but I cant quite figure it out and I keep going in circles.

Here is what I've tried:

let newArr = [];
response.survey.map((ballot, i) => {
    for (const key in ballot) {
        if (newArr.length == 0) {
            let newObj = {
                name: pos[key].user,
                points: pos[key].points
            }
            newArr.push(newObj);
        } else {
            for (let k = 0; k < newArr.length; k  ) {
                if (newArr[k].name === pos[key].name) {
                    newArr[k].points  = pos[key].points;
                } else {
                    if (k   1 == newArr.length) {
                        let newObj = {
                            name: pos[key].name,
                            points: pos[key].points
                        }
                        newArr.push(newObj);
                    }
                }
            }
        }
    }
}

I think I've been working on this issue for so long that I've started to go into circles and heading down the wrong path.

Any help would be greatly appreciated.

CodePudding user response:

Here is one solutions that could work:

const response = [
  [
  {
    1: { id: 1, user: "user_1", points: 5 },
    2: { id: 2, user: "user_2", points: 3 },
  }
],
[
  {
    1: { id: 1, user: "user_1", points: 10 },
    2: { id: 2, user: "user_2", points: 0 },
  }
]
]


const newArr = [];

for(let index in response){
  const object = response[index][0]
  for(let index2 in object){
    const index = newArr.findIndex(val => val.user === object[index2].user)
    if(index >= 0){
      newArr[index].points  = object[index2].points;

    }else{
      newArr.push({user:object[index2].user, points: object[index2].points })
    }
  }
}

CodePudding user response:

This solved my problem

 await fetch('/api/getBallots')
            .then((response) => response.json())
            .then(async (data) => {
                let newArray = []

                data.ballots.map((ballot, i) => {
                    for (const key in ballot) {
                        if(!ballot[key].name) {
                            return;
                        } else {
                            let newObj = {
                                name: ballot[key].name,
                                points: ballot[key].points
                            }
                            newArray.push(newObj);
                        }
                        
                    }
                });
                let rankings = []; 
                for (let i = 0; i < newArray.length; i  ) {
                    if (rankings.length == 0) {
                        rankings.push({
                            name: newArray[i].name,
                            points: newArray[i].points
                        });
                    } else {
                        const index = rankings.findIndex((item) => item.name == newArray[i].name);
                        if (index == -1) {
                            rankings.push({
                                name: newArray[i].name,
                                points: newArray[i].points
                            })
                        } else {
                            rankings[index].points  = newArray[i].points;
                        }
                    }
                }          
        })
  • Related