📜  return where an property eqauls - 不管(1)

📅  最后修改于: 2023-12-03 15:19:49.038000             🧑  作者: Mango

Return where a property equals - regardless

Introduction

When working with arrays or objects in JavaScript, it's common to need to find specific items that match certain criteria. One common scenario is to return all the items that have a specific property equal to a certain value. This can be achieved using various methods in JavaScript, such as Array.filter(), Array.reduce(), and for loops.

Array.filter()

Array.filter() is a built-in method in JavaScript that creates a new array with all elements that pass the test implemented by the provided function. We can use this method to return all the items in an array that have a specific property equal to a certain value.

const items = [
  { id: 1, name: 'Item 1', status: 'active' },
  { id: 2, name: 'Item 2', status: 'inactive' },
  { id: 3, name: 'Item 3', status: 'active' },
  { id: 4, name: 'Item 4', status: 'inactive' },
];

const filteredItems = items.filter(item => item.status === 'active');

console.log(filteredItems);
// Output: [{ id: 1, name: 'Item 1', status: 'active' }, { id: 3, name: 'Item 3', status: 'active' }]

In the example above, we used Array.filter() to create a new array called filteredItems that contains only the items from the original array that have a status property equal to 'active'.

Array.reduce()

Array.reduce() is another built-in method in JavaScript that applies a function to each element of an array in order to reduce the array to a single value. We can use this method to return an array of items that have a specific property equal to a certain value.

const items = [
  { id: 1, name: 'Item 1', category: 'A' },
  { id: 2, name: 'Item 2', category: 'B' },
  { id: 3, name: 'Item 3', category: 'A' },
  { id: 4, name: 'Item 4', category: 'C' },
];

const filteredItems = items.reduce((acc, item) => {
  if (item.category === 'A') {
    acc.push(item);
  }
  return acc;
}, []);

console.log(filteredItems);
// Output: [{ id: 1, name: 'Item 1', category: 'A' }, { id: 3, name: 'Item 3', category: 'A' }]

In the example above, we used Array.reduce() to create a new array called filteredItems that contains only the items from the original array that have a category property equal to 'A'.

For loop

For loops are a common way to iterate over an array or object in JavaScript. We can use a for loop to iterate over an array and return only the items that have a specific property equal to a certain value.

const items = [
  { id: 1, name: 'Item 1', type: 'A' },
  { id: 2, name: 'Item 2', type: 'B' },
  { id: 3, name: 'Item 3', type: 'A' },
  { id: 4, name: 'Item 4', type: 'C' },
];

const filteredItems = [];

for (let i = 0; i < items.length; i++) {
  if (items[i].type === 'A') {
    filteredItems.push(items[i]);
  }
}

console.log(filteredItems);
// Output: [{ id: 1, name: 'Item 1', type: 'A' }, { id: 3, name: 'Item 3', type: 'A' }]

In the example above, we used a for loop to iterate over the items array and return only the items that have a type property equal to 'A'.

Conclusion

In summary, there are multiple ways to return all the items that have a specific property equal to a certain value in JavaScript. We can use Array.filter(), Array.reduce(), or for loops to achieve this. Each method has its own advantages and disadvantages, so it's important to choose the one that suits our needs the best.