简体   繁体   中英

Got an array of 2 objects.Each object has a property `events` which contains an array of objects. How to filter elements whose participantId is not 1?

I have an array of 2 objects. Each object has a property called events which contains an array of objects. Each one of those event objects have a property called participantId .

I'm trying to use filter in order to filter out all the objects inside the events property whose participantId is not 1 but I'm having some difficulties because of how confusing this whole object is (at least to me).

I've been trying all kinds of filter, find, map and reduce variations for some time now and I just couldn't figure it out so I can safely assume I just have no idea how to. Here's how the object looks like:

(2) [{…}, {…}]
0: {participantFrames: {…}, events: Array(47), timestamp: 60032}
    events: Array(3)
        0: {type: "ITEM_PURCHASED", timestamp: 5481, participantId: 8, itemId: 3858}
        1: {type: "ITEM_PURCHASED", timestamp: 6207, participantId: 1, itemId: 2003}
        2: {type: "ITEM_PURCHASED", timestamp: 6339, participantId: 6, itemId: 2003}
1: {participantFrames: {…}, events: Array(7), timestamp: 120034}
    events: Array(3)
        0: {type: "ITEM_PURCHASED", timestamp: 7842, participantId: 5, itemId: 5485}
        1: {type: "ITEM_PURCHASED", timestamp: 8568, participantId: 2, itemId: 2543}
        2: {type: "ITEM_PURCHASED", timestamp: 9054, participantId: 1, itemId: 2045}

And I'm hoping to end up with something like this after the function does its thing:

(2) [{…}, {…}]
0: {participantFrames: {…}, events: Array(47), timestamp: 60032}
    events: Array(1)
        1: {type: "ITEM_PURCHASED", timestamp: 6207, participantId: 1, itemId: 2003}
1: {participantFrames: {…}, events: Array(7), timestamp: 120034}
    events: Array(1)
        2: {type: "ITEM_PURCHASED", timestamp: 9054, participantId: 1, itemId: 2045}

Just use filter()

 const data = [ { participantFrames: 'some value', events: [ {type: "ITEM_PURCHASED", timestamp: 5481, participantId: 8, itemId: 3858}, {type: "ITEM_PURCHASED", timestamp: 6207, participantId: 1, itemId: 2003}, {type: "ITEM_PURCHASED", timestamp: 6339, participantId: 6, itemId: 2003} ], timestamp: 60032 }, { participantFrames: 'some value', events: [ {type: "ITEM_PURCHASED", timestamp: 7842, participantId: 5, itemId: 5485}, {type: "ITEM_PURCHASED", timestamp: 8568, participantId: 2, itemId: 2543}, {type: "ITEM_PURCHASED", timestamp: 9054, participantId: 1, itemId: 2045} ], timestamp: 120034 } ]; const output = data.map(item => { return { ...item, events: item.events.filter(e => e.participantId === 1) }; }); console.log(output); // Or const output2 = data.map(function (item) { const events = item.events.filter(function(event) { return event.participantId === 1; }); return Object.assign(item, { events: events }); }); console.log(output2);

You can use filter to accomplish this:

a = [ { pid: 1 }, { pid: 2 }, { pid: 1 } ]
a.filter(e => e.pid === 1)

> [ { pid: 1 }, { pid: 1 } ]

Maybe this work:

  const newArray = data.map(arr => {
     return arr.map(val => {
        return val.participantId == 1
     })
  })

You can use Array.filter() to find all events with participantId that is 1.

 const events = [ {type: "ITEM_PURCHASED", timestamp: 7842, participantId: 5, itemId: 5485}, {type: "ITEM_PURCHASED", timestamp: 8568, participantId: 2, itemId: 2543}, {type: "ITEM_PURCHASED", timestamp: 9054, participantId: 1, itemId: 2045} ]; const result = events.filter((row) => row.participantId == 1); console.log(result);

Look at this codesandbox It looks like you have to iterate through multiple arrays.

const data = [
  {
    events: [
      {
        type: "ITEM_PURCHASED",
        timestamp: 5481,
        participantId: 8,
        itemId: 3858
      },
      {
        type: "ITEM_PURCHASED",
        timestamp: 6207,
        participantId: 1,
        itemId: 2003
      },
      {
        type: "ITEM_PURCHASED",
        timestamp: 6339,
        participantId: 6,
        itemId: 2003
      }
    ]
  },
  {
    events: [
      {
        type: "ITEM_PURCHASED",
        timestamp: 7842,
        participantId: 5,
        itemId: 5485
      },
      {
        type: "ITEM_PURCHASED",
        timestamp: 8568,
        participantId: 2,
        itemId: 2543
      },
      {
        type: "ITEM_PURCHASED",
        timestamp: 9054,
        participantId: 1,
        itemId: 2045
      }
    ]
  }
];

const filteredData = [];
data.forEach(d => {
  d.events.forEach(e => {
    if (e.participantId !== 1) {
      filteredData.push(e);
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM