0

Attempting to build an array, which outputs data as following:

  • Healthcare
    -- Data-driven insights to improve healthcare
    -- Urban Analytics

  • Transport
    -- Urban Analytics

  • Cities
    -- Urban Analytics

I've tried looping 'expertise'/'text' but I'm unable to get them to work together to get the desired output within the console.log

Any and all help would be highly appreciated.

    var items = [{
            "item": {
                "id": 0,
                "sector": 'Data',
                "expertise": ["Healthcare"],
                "text": "Data-driven insights to improve healthcare"
            }
        },
        {
            "item": {
                "id": 1,
                "sector": 'Data',
                "expertise": ["Healthcare", "Transport", "Cities"],
                "text": "Urban Analytics"
            }
        }
    }];

    var array = [];

    for (var i = 0; i < items.length; i++) {

        var arr = [{
            'title': items[i].item.sector,
            'items': []
        }];

        for (var j = 0, b = items[i].item.expertise.length; j < b; j++) {
            if (items[i].item.expertise[j] == expertise) {

                arr[0]['items'].push({
                    'list': items[i].item.text
                });

            }
        }

        array.push(arr);

    }

    console.log(array);
Adam W.
  • 33
  • 5

3 Answers3

0

I would start by building an object of the relevant expertise and text lists, and then outputting however you want. For example:

var items = [{
  "item": {
    "id": 0,
    "sector": 'Data',
    "expertise": ["Healthcare"],
    "text": "Data-driven insights to improve healthcare"
  }
}, {
  "item": {
    "id": 1,
    "sector": 'Data',
    "expertise": ["Healthcare", "Transport", "Cities"],
    "text": "Urban Analytics"
  }
}]
    
const obj = items.reduce((res, { item }) => {
  item.expertise.forEach(e => {
    res[e] = res[e] || []
    if(res[e].indexOf(item.text) < 0){ res[e].push(item.text) }
  })
  return res
}, {})

Object.keys(obj).forEach(k => {
  console.log(k)
  obj[k].forEach(text => console.log(`-- ${text}`))
})
Matt Way
  • 32,319
  • 10
  • 79
  • 85
0

You should use map

const array =  items.map(el => ({
    text: el.item.sector,
    items: [...el.item.expertise, {
        list : el.item.text
    }]
});
0

Here you have another approach with reduce(), also using destructuring and the spread operator

var items = [
  {
    "item": {
      "id": 0,
      "sector": 'Data',
      "expertise": ["Healthcare"],
      "text": "Data-driven insights to improve healthcare"
    }
  },
  {
    "item": {
      "id": 1,
      "sector": 'Data',
      "expertise": ["Healthcare", "Transport", "Cities"],
      "text": "Urban Analytics"
    }  
  }
];

let res = items.reduce((acc, {item: {expertise, text}}) =>
{
     expertise.forEach(x => acc[x] = [...(acc[x] || []), text]);
     return acc;
}, {});


Object.entries(res).forEach(([k, v]) =>
{
    console.log(k + "\n->" + v.join("\n->"));
});
Shidersz
  • 16,846
  • 2
  • 23
  • 48