0

const merged = [
  [
    {
      _id: "6136096f4255d84bcb4a7144",
      user_id: "5fbfa729fc46a415ce5503a6",
      device_platform: 'ios',
    },
    { user: [Object] }
  ],
  [
    {
      _id: "613609414255d84bcb4a7122",
      user_id: "5fbf6f91aff7f3320a906547",
      device_platform: 'ios',
    },
    { user: [Object] }
  ],
  [
    {
      _id: "613709f49223350dfdaec618",
      user_id: "5fbfa748fc46a415ce5505f1",
      device_platform: 'ios',
    },
    {
      _id: "613609184255d84bcb4a710a",
      user_id: "5fbfa748fc46a415ce5505f1",
      device_platform: 'ios',
    },
    { user: [Object] }
  ]
]

const user_id = ["5fbfa748fc46a415ce5503a8"];

const { matching, nonMatching } = merged.reduce(
    (acc, userRecord) => {
      userRecord.JSON.Stringify(user._id) &&
      userRecord.JSON.Stringify(user._id).some(_id => user_id.includes(_id))
        ? acc.matching.push(userRecord)
        : acc.nonMatching.push(userRecord);
      return acc;
    },
    { matching: [], nonMatching: [] }
  );

How to filter out from nested array is there any way to fix it with this response please guide

user is coming at object it must contain _id for this I used JSON.Stringify() which seems work isn't ?

Thanks

4
  • do you need delete nested array like that [ [ { data } ] ] => [ { data } ] Commented Sep 7, 2021 at 8:17
  • it is not quite clear, what exactly you want to filter out from nested arrays, can you, please, share some (valid) input along with expected output? Commented Sep 7, 2021 at 8:44
  • user object is like { _id : "5fbfa729fc46a415ce5503a6", name : "xys" } now if anything match with user._id == user_id it should be in match case otherwise will be in unmatch case currently it is coming [Object] might need to use JSON.Strintify() Commented Sep 7, 2021 at 8:48
  • There is no JSON.Stringify() — it is JSON.stringify() Commented Sep 7, 2021 at 20:26

1 Answer 1

1

You do not need JSON.stringify() to process your data structures, you can use Optional chaining (?.) here. However, you can use JSON.stringify() for the full output:

const merged = [
  [
    {
      _id: "6136096f4255d84bcb4a7144",
      user_id: "5fbfa729fc46a415ce5503a6",
      device_platform: 'ios',
    },
    { user: { _id : "5fbfa729fc46a415ce5503a6", name : "xys" } }
  ],
  [
    {
      _id: "613609414255d84bcb4a7122",
      user_id: "5fbf6f91aff7f3320a906547",
      device_platform: 'ios',
    },
    { user: { _id : "5fbfa729fc46a415ce5503a6", name : "xys" } }
  ],
  [
    {
      _id: "613709f49223350dfdaec618",
      user_id: "5fbfa748fc46a415ce5505f1",
      device_platform: 'ios',
    },
    {
      _id: "613609184255d84bcb4a710a",
      user_id: "5fbfa748fc46a415ce5505f1",
      device_platform: 'ios',
    },
    { user: { _id : "5fbfa748fc46a415ce5503a8", name : "xys" } }
  ]
];

const user_id = ["5fbfa748fc46a415ce5503a8"];

const { matching, nonMatching } = merged.reduce(
    (acc, userRecord) => {
      if (userRecord.some(object => user_id.includes(object?.user?._id))) {
        acc.matching.push(userRecord);
      } else {
        acc.nonMatching.push(userRecord);
      }
      return acc;
    },
    { matching: [], nonMatching: [] }
);

console.log(JSON.stringify(matching, null, '  '));
console.log(JSON.stringify(nonMatching, null, '  '));

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.