Home > Back-end >  How to search an array of data items for a specifically matching item?
How to search an array of data items for a specifically matching item?

Time:03-02

I need to do a search in the below provided array so that when I search for a pin value of '002' I should get the area value of 'F02'.

[{
  pin: "001",
  area: "F01",
}, {
  pin: "002",
  area: "F02",
}, {
  pin: "003",
  area: "F07",
}, {
  pin: "004",
  area: "F08",
}]

CodePudding user response:

Use array.filter:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const arr = [{
  pin: "001",
  area: "F01",
}, {
  pin: "002",
  area: "F02",
}, {
  pin: "003",
  area: "F07",
}, {
  pin: "004",
  area: "F08",
}];

// the resulting array of a successful filter process.
console.log(
  'the resulting array of a successful filter process ...',
  arr.filter(i => i.pin === '002')
);
// accessing the `area` property of the first matching item.
console.log(
  'accessing the `area` property of the first matching item ...',
  arr.filter(i => i.pin === '002')[0].area
);
console.log('\n');

// the resulting array of a non matching filter process.
console.log(
  'the resulting array of a non matching filter process ...',
  arr.filter(i => i.pin === '005')
);
// safely accessing the not assured first items's `area` property of an unknow filter result.
console.log(
  "safely accessing the not assured first items's `area` property of an unknow filter result ...",
  arr.filter(i => i.pin === '005')[0]?.area
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Use array.find:

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

const arr = [{
  pin: "001",
  area: "F01",
}, {
  pin: "002",
  area: "F02",
}, {
  pin: "003",
  area: "F07",
}, {
  pin: "004",
  area: "F08",
}];

// object which satisfies the condition does exist.
console.log(
  'object which satisfies the condition does exist ...',
  arr.find(i => i.pin === '002')
);
// accessing the `area` property of the well known return value.
console.log(
  'accessing the `area` property of the well known return value ...',
  arr.find(i => i.pin === '002').area
);
console.log('\n');

// object which satisfies the condition does not exist.
console.log(
  'object which satisfies the condition does not exist ...',
  arr.find(i => i.pin === '005')
);
// safely accessing the possible `area` property of a varying/unsafe return value.
console.log(
  'safely accessing the possible `area` property of a varying/unsafe return value ...',
  arr.find(i => i.pin === '005')?.area
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

If you want to find a specific item, always consider use the array.find since its orginal goal is to find a sepecific item in the array, but the goal of array.filter is to use run a test to all item in array and return the pass item.

Important note by Peter Seliger:

find stops iterating the processed array as soon as the condition matches, whereas filter always does a full loop

Array.find on your situation will have much better performance speed and save your calculation time since array.find will immediately return the item you find, but array.filter will iterate all the item in the array whatever

CodePudding user response:

You could just loop through the array and find the matches:
`var results = [];
 var searchField = "pin";
 var searchVal = "area";
 for (var i=0 ; i < obj.list.length ; i  )
 {
    if (obj.list[i][searchField] == searchVal) {
        results.push(obj.list[i]);
 }
}`
  • Related