skip to Main Content

For example I have this JSON data:

const data = [
{
  title: 'Nike Dunk Low Panda',
  sku: 'DD1391-100',
  size: '9 US',
},
{
  title: 'Nike Dunk Low Panda',
  sku: 'DD1391-100',
  size: '10 US',
},
{
  title: 'Nike Dunk Low Panda',
  sku: 'DD1391-100',
  size: '11 US',
},
]

How to create another array like:

const data = [
{
  title: 'Nike Dunk Low Panda',
  sku: 'DD1391-100',
  size: '9 US, 10 US, 11 US'
}

]

I’m new in JS, trying to learn it )

3

Answers


  1. const data = [{
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '9 US',
      },
      {
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '10 US',
      },
      {
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '11 US',
      },
    ]
    
    sizeList = []
    
    for (x of data) {
      sizeList.push(x['size'])
    }
    
    console.log({
      title: 'Nike Dunk Low Panda',
      sku: 'DD1391-100',
      size: sizeList
    })
    Login or Signup to reply.
  2. One way to do it is to create a new array by aggregating the sizes for each unique combination of title and sku

    • Create a map to store aggregated data
    • Iterate over the original data
    • Generate a unique key based on title and sku
    • Check if the key exists in the map
    • Append the size to the existing entry
    • Create a new entry in the map
    • Convert the map values to an array
    const originalData = [
      {
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '9 US',
      },
      {
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '10 US',
      },
      {
        title: 'Nike Dunk Low Panda',
        sku: 'DD1391-100',
        size: '11 US',
      },
    ];
    
    const aggregatedMap = new Map();
    
    originalData.forEach((item) => {
      const key = `${item.title}-${item.sku}`;
    
      if (aggregatedMap.has(key)) {
        aggregatedMap.get(key).size += `, ${item.size}`;
      } else {
        aggregatedMap.set(key, { title: item.title, sku: item.sku, size: item.size });
      }
    });
    
    //result
    const newData = Array.from(aggregatedMap.values());
    
    console.log(newData);
    Login or Signup to reply.
  3. You could group by two keys and map the result by getting the wanted property from the group.

    const
        data = [{ title: 'Nike Dunk Low Panda', sku: 'DD1391-100', size: '9 US' }, { title: 'Nike Dunk Low Panda', sku: 'DD1391-100', size: '10 US' }, { title: 'Nike Dunk Low Panda', sku: 'DD1391-100', size: '11 US' }],
        keys = ['title', 'sku'],
        result = Object
            .values(Object.groupBy(data, o => keys.map(k => o[k]).join('|')))
            .map(a => ({ ...a[0], size: a.map(({ size }) => size).join(', ') }));
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search