0

I have two arrays, slicesRank and slicesCount with the following structure. Each element has id and value, which is an array of 46. Values is composed of date and measurement.

e.g.

sliceRank[0]:   
{
    id: Catan = rank, 
    values(Array(46)) : {date, measurement}
}


sliceCount[0]:
{
    id: Catan=count, 
    values(Array(46)) : {date, measurement}
}

What should I do if I want to combine the elements with the same prefix in id names. For example, the first element in this two arrays.

The desired structure would be

{
    id: Catan, 
    values(Array(46)) : {date, count, rank}
}

I tried the following, but the values shows undefined.

for(i = 0; i < slicesRank.length; i++) {
    var newElement = {};
    newElement['id'] = slicesRank[i].id.replace('=rank', '');
    newElement['values'] = {
      date: slicesRank[i].date,
      rank: slicesRank[i].measurement,
      count: slicesCount[i].measurement
    };
    sliceNew.push(newElement);
};

The structure of slicesRank is like this:

enter image description here

enter image description here

2
  • 2
    {id: Catan=rank, values(Array(46)) : {date, measurement} } is not valid syntax. It's not clear what the data structure you're working with is Commented Mar 7, 2021 at 18:55
  • @CertainPerformance Hi, sorry to any confusion caused here. I'm new to JS. I added two screenshot trying to show the structure of slicesRank here, would that help? Commented Mar 7, 2021 at 19:01

3 Answers 3

1

Here's a sample transformation that you can try:

const combined = slicesRank.map((rank) => {
  const id = rank.id.replace("=rank", "");
  const count = slicesCount.find(
    (count) => count.id.replace("=count", "") === id
  );
  return {
    id,
    values: rank.values.map((val, index) => ({
      date: val.date,
      rank: val.measurement,
      count: count?.values[index].measurement
    }))
  };
});

Here's a working example https://codesandbox.io/s/awesome-lovelace-e31zj?file=/src/index.js

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

Comments

1

By slice I think you mean Array.

const combinedArray = sliceRank.map((sliceItem, itemIndex) => {
    return {
        id,
        values: sliceItem.values.map(({ date, measurement }, valueIndex) => ({
            date,
            count: sliceCount[itemIndex][valueIndex].measurement,
            rank: measurement,
        }))
    }
})

This solution returns an array of objects combined using sliceRank and sliceCount. So now each array item now contains values of structure { date, count, rank }. Array.map is immutable so both slices will be the same.

Comments

0

You can try this.

let sliceRank=[{id: "Catan=rank",  values : [{"date":"10/10/2020", "measurement":120} ]}];
let sliceCount=[{id: "Catan=count",  values :[{"date":"10/10/2020", "measurement":20} ] }];

let newData=[];

const data = sliceRank.map((slice, sliceIndex) => {
    return {
        id:slice.id.split("=")[0],
        values: slice.values.map(({ date, measurement }, valueIndex) => ({
            date,            
            rank: measurement,
            count: sliceCount[sliceIndex].values.map(r=> r.measurement)[0],
        }))
    }
})

console.log(data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.