Mohammad Khan
Mohammad Khan

Reputation: 363

Filtering an array inside an array

I'm attempting to filter info based on a favorite.userId. This is what I'd want, however throughout my experiment, I got an empty array.

I'm attempting to map the favorite as indicated below. favorite.userId, and filtering it with user to retrieve data with the same user Id or the inputted data. Please let me know, or I'll clarify what I'm talking about.

const data = [{
  name: "Alif",
  favorites: [{
      _id: "60fe705efc8be22860620d3b",
      userId: "60eaa1b020fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
    {
      _id: "60fe125efc8be22860620d3b",
      userId: "60eaa19820fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
  ],
}, ];

const user = {
  id: "60eaa1b020fa782758751285",
};

const favoriteUser = data.map(({
  favorites
}) => {
  return favorites.map((favorite) => {
    return favorite.userId;
  });
});

const wishlist = data.filter(() => {
  return favoriteUser == user.id;
});

console.log(wishlist);

Upvotes: 0

Views: 78

Answers (3)

Harshil Khamar
Harshil Khamar

Reputation: 216

const data = [
        {
            name: "Alif",
            favorites: [
                {
                  _id: "60fe705efc8be22860620d3b",
                  userId: "60eaa1b020fa782758751285",
                  username: "Alif",
                  createdAt: "2021-07-26T08:20:46.522Z",
                },
                {
                  _id: "60fe125efc8be22860620d3b",
                  userId: "60eaa19820fa782758751285",
                  username: "Alif",
                  createdAt: "2021-07-26T08:20:46.522Z",
                },
            ],
        },
    ];

const user = {
  id: "60eaa1b020fa782758751285",
};

var favoriteUser = data.map(function(value){
    return value['favorites'].map(function(value){
        return value['userId'];
    });
});

const wishlist = data.filter(function(value) {
  return check(user['id']);
});

function check(value)
{
    flag = false;
    favoriteUser.forEach(
        function(Value1)
        {
                Value1.forEach(
                function(userValue)
                {
                    if(value == userValue)
                    {
                        flag=true;
                    }
                }
            )
        }
    )
    return flag;
}
console.log(wishlist);

  • I think there is some error in your logic refer above code.

  • I used function favouriteUser to fetch all the user IDs and use filter function of array to filter the required user.

  • I implemented check function to check whether the UserID is match or not for filter function

  • Hope this will solve your problem.

Upvotes: 0

jiwopene
jiwopene

Reputation: 3627

You can use this:

const data = [{
  name: "Alif",
  favorites: [{
      _id: "60fe705efc8be22860620d3b",
      userId: "60eaa1b020fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
    {
      _id: "60fe125efc8be22860620d3b",
      userId: "60eaa19820fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
  ],
}, ];

const user = {
  id: "60eaa1b020fa782758751285",
};

const wishlist = data.map(({
  favorites
}) => favorites.filter(favorite => favorite.userId == user.id)).flat();

console.log(wishlist);

The map call produces array of favorite arrays which we then filter by the user ID. We can then flatten the arrays.


You can also move the flat call to the middle of map and filter. Then the filter call must happen on the flattended array. This also results in more readable code.

const data = [{
  name: "Alif",
  favorites: [{
      _id: "60fe705efc8be22860620d3b",
      userId: "60eaa1b020fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
    {
      _id: "60fe125efc8be22860620d3b",
      userId: "60eaa19820fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
  ],
}, ];

const user = {
  id: "60eaa1b020fa782758751285",
};

const wishlist = data
    .map(({ favorites }) => favorites)
    .flat()
    .filter(favorite => favorite.userId == user.id);

console.log(wishlist);

The data flow visualized:

data flow image

Upvotes: 2

mplungjan
mplungjan

Reputation: 177885

Perhaps something like this?

It is not clear what you want to find after finding the favourite user

const data = [{
  name: "Alif",
  favorites: [{
      _id: "60fe705efc8be22860620d3b",
      userId: "60eaa1b020fa782758751285",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
    {
      _id: "60fe125efc8be22860620d3b",
      userId: "60eaa19820fa782758751286",
      username: "Alif",
      createdAt: "2021-07-26T08:20:46.522Z",
    },
  ],
}, ];

const user = {
  id: "60eaa1b020fa782758751285",
};

const favoriteUser = data[0].favorites.map((favorite) => {
  return favorite.userId;
}); // this just confirms we have it

console.log(favoriteUser);

const wishlist = data.filter(item => {
  return favoriteUser.includes(user.id);
});

console.log(wishlist);

Upvotes: 0

Related Questions