Filter the array of objects

How can I filter arrayObjects to return ["test1", "test2", "test3"]

const arrayObjects = [{
  date: "2021-02-17",
  someAction: [{
    id: "1",
    metatags: {
      addedBy: "test1"
    }
  }, {
    id: "2",
    metatags: {
      addedBy: "test2"
    }
  }]
}, {
  date: "2021-02-16",
  someAction: [{
    id: "3",
    metatags: {
      addedBy: "test1"
    }
  }]
}, {
  date: "2021-02-15",
  someAction: [{
    id: "4",
    metatags: {
      addedBy: "test3"
    }
  }, {
    id: "5",
    metatags: {
      addedBy: "test2"
    }
  }]
}]

I`m writing something like this:

arrayObjects?.map(arr => arr.someAction.map(a=> a.metatags.addedBy))

4 thoughts on “Filter the array of objects”

  1. Not sure I understand completely, but this snippet will create a new array containing the addedBy data for each action nested in the parent object.

    let addedBy = []
    
    arrayObjects.forEach(obj => {
      obj.someAction.forEach(action => {
        addedBy.push(action.metatags.addedBy)
      })
    })
    
    Reply
  2. what you need to use is reduce which will help you to create the desired output.

    const arrayObjects = [{
      date: "2021-02-17",
      someAction: [{
        id: "1",
        metatags: {
          addedBy: "test1"
        }
      }, {
        id: "2",
        metatags: {
          addedBy: "test2"
        }
      }]
    }, {
      date: "2021-02-16",
      someAction: [{
        id: "3",
        metatags: {
          addedBy: "test1"
        }
      }]
    }, {
      date: "2021-02-15",
      someAction: [{
        id: "4",
        metatags: {
          addedBy: "test3"
        }
      }, {
        id: "5",
        metatags: {
          addedBy: "test2"
        }
      }]
    }]
    
    const reducer = (accum, cv) => {
      const actions = cv.someAction;
    
      actions.forEach(action => {
        const meta = action.metatags;
        const addedBy = meta.addedBy;
    
        const index = accum.indexOf(addedBy);
    
        if (index === -1) {
          accum.push(addedBy)
        }
      })
    
      return accum;
    }
    
    const result = arrayObjects.reduce(reducer, []);
    
    console.log(result)
    Reply
  3. Firsly you need to flat the result and then pick unique items.
    To flat an array you can use flat array method, and for gathering unique results I’d suggest you to use Set data structure (by definition it has only unique items) and the cast to array using Array.from

    Array.from(new Set(arrayObjects?.map(arr => arr.someAction.map(a=> a.metatags.addedBy)).flat()))
    

    Probably, looks pretty complex, but works as a charm 🙂

    Reply
  4. May ways to attack it. I would use Object.keys, reduce with and object, and forEach

    const arrayObjects = [{
      date: "2021-02-17",
      someAction: [{
        id: "1",
        metatags: {
          addedBy: "test1"
        }
      }, {
        id: "2",
        metatags: {
          addedBy: "test2"
        }
      }]
    }, {
      date: "2021-02-16",
      someAction: [{
        id: "3",
        metatags: {
          addedBy: "test1"
        }
      }]
    }, {
      date: "2021-02-15",
      someAction: [{
        id: "4",
        metatags: {
          addedBy: "test3"
        }
      }, {
        id: "5",
        metatags: {
          addedBy: "test2"
        }
      }]
    }]
    
    const users = Object.keys(arrayObjects.reduce((acc, obj) => {
      obj.someAction.forEach(action => acc[action.metatags.addedBy] = 1)
      return acc;
    }, {}));
    
    console.log(users);

    If you want to go the map() route. You would need to use two maps, flat, and a set to get them deduped

    const arrayObjects = [{
      date: "2021-02-17",
      someAction: [{
        id: "1",
        metatags: {
          addedBy: "test1"
        }
      }, {
        id: "2",
        metatags: {
          addedBy: "test2"
        }
      }]
    }, {
      date: "2021-02-16",
      someAction: [{
        id: "3",
        metatags: {
          addedBy: "test1"
        }
      }]
    }, {
      date: "2021-02-15",
      someAction: [{
        id: "4",
        metatags: {
          addedBy: "test3"
        }
      }, {
        id: "5",
        metatags: {
          addedBy: "test2"
        }
      }]
    }];
    
    const users = [...new Set(arrayObjects.map(obj => {
      return obj.someAction.map(action => action.metatags.addedBy)
    }).flat())];
    
    console.log(users);
    Reply

Leave a Comment