its me
its me

Reputation: 542

How can i search inside array of array object in javascript?

I have a booking array of arrays that contain objects and I have to search inside the array using searchValue could you please help me here

here we have to check the booking id if booking id and searchValue matched we have to push that object into result array.

let bookingArr = [
  [{
      name: "user 1",
      bookingid: 10,
      product: "ab"
    },
    {
      name: "user 1",
      bookingid: 10,
      product: "cd"
    }
  ],
  [{
      name: "user 2",
      bookingid: 11,
      product: "ui"
    },
    {
      name: "user 1",
      bookingid: 10,
      product: "ef"
    }
  ],
  [{
      name: "user 3",
      bookingid: 12,
      product: "ui"
    },
    {
      name: "user 4",
      bookingid: 13,
      product: "ef"
    }
  ]
];


var searchValue = "10,11";

var FOUND = bookingArr.find(function(post, index) {
  if (post.bookingid == 11)
    return true;
});


console.log(FOUND)

expected result

[ { name:"user 1", bookingid:10, product: "ab" },
    { name:"user 1", bookingid:10, product: "cd" },
    { name:"user 1", bookingid:10, product: "ef" },
    { name:"user 2", bookingid:11, product: "ui" }]

Upvotes: 2

Views: 201

Answers (5)

Som Shekhar Mukherjee
Som Shekhar Mukherjee

Reputation: 8158

You can simply flatten the array and filter out the desired results. Also, I've create a Set out of the search value for making the code more performant.

let bookingArr = [
  [
    { name: 'user 1', bookingid: 10, product: 'ab' },
    { name: 'user 1', bookingid: 10, product: 'cd' },
  ],
  [
    { name: 'user 2', bookingid: 11, product: 'ui' },
    { name: 'user 1', bookingid: 10, product: 'ef' },
  ],
  [
    { name: 'user 3', bookingid: 12, product: 'ui' },
    { name: 'user 4', bookingid: 13, product: 'ef' },
  ],
];

const searchValue = "10,11"

const searchSet = new Set(searchValue.split(","))

const res = bookingArr.flat().filter(b => searchSet.has(String(b.bookingid)))

console.log(res)

Upvotes: 1

Elson Ramos
Elson Ramos

Reputation: 820

You can try this:

let bookingArr = [
  [
    { name: 'user 1', bookingid: 10, product: 'ab' },
    { name: 'user 1', bookingid: 10, product: 'cd' },
  ],
  [
    { name: 'user 2', bookingid: 11, product: 'ui' },
    { name: 'user 1', bookingid: 10, product: 'ef' },
  ],
  [
    { name: 'user 3', bookingid: 12, product: 'ui' },
    { name: 'user 4', bookingid: 13, product: 'ef' },
  ],
];

var searchValue = '10,11';

var searchIds = searchValue.split(',');

var FOUND = [];

bookingArr.forEach((el) => {
  el.forEach((el2) => {
    searchIds.forEach((num) => {
      if (num == el2.bookingid) {
        FOUND.push(el2);
      }
    });
  });
});

console.log(FOUND);

Upvotes: 1

Lakshya Thakur
Lakshya Thakur

Reputation: 8318

Following with filter and Set will work :-

let bookingArr = [
  [{
      name: "user 1",
      bookingid: 10,
      product: "ab"
    },
    {
      name: "user 1",
      bookingid: 10,
      product: "cd"
    }
  ],
  [{
      name: "user 2",
      bookingid: 11,
      product: "ui"
    },
    {
      name: "user 1",
      bookingid: 10,
      product: "ef"
    }
  ],
  [{
      name: "user 3",
      bookingid: 12,
      product: "ui"
    },
    {
      name: "user 4",
      bookingid: 13,
      product: "ef"
    }
  ]
];


var searchValue = "10,11";

function logic(arr, searchValue) {
  let searchInputs = searchValue.split(',');
  let searchSet = new Set(searchInputs.map(val => parseInt(val)));
  let output = bookingArr.flat().filter(curr => searchSet.has(curr.bookingid))
  return output;
}


console.log(logic(bookingArr, searchValue));

Upvotes: 1

salkcid
salkcid

Reputation: 99

You can try this code:

const bookingArr = [[
  { name: 'user 1', bookingid: 10, product: 'ab' },
  { name: 'user 1', bookingid: 10, product: 'cd' },
], [
  { name: 'user 2', bookingid: 11, product: 'ui' },
  { name: 'user 1', bookingid: 10, product: 'ef' },
],
[
  { name: 'user 3', bookingid: 12, product: 'ui' },
  { name: 'user 4', bookingid: 13, product: 'ef' },
]];


const searchValue = '10,11';

const sv = searchValue.split(',').map(Number);

const FOUND = bookingArr.flat().filter(({ bookingid }) => sv.includes(bookingid));

console.log(FOUND);

Upvotes: 3

Unmitigated
Unmitigated

Reputation: 89204

You can use Array#filter with a Set for better performance after flattening the array.

let bookingArr = [[
    { name:"user 1", bookingid:10, product: "ab" },
    { name:"user 1", bookingid:10, product: "cd" }
],[
    { name:"user 2", bookingid:11, product: "ui" },
    { name:"user 1", bookingid:10, product: "ef" }
],
[
    { name:"user 3", bookingid:12, product: "ui" },
    { name:"user 4", bookingid:13, product: "ef" }
]];
let searchValue = "10,11";
let set = new Set(searchValue.split(",").map(Number)); // for faster lookup
let res = bookingArr.flat().filter(x => set.has(x.bookingid));
console.log(res);

Upvotes: 2

Related Questions