Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

553
Views
TypeScript - Take object out of array based on attribute value

My array looks like this:

array = [object {id: 1, value: "itemname"}, object {id: 2, value: "itemname"}, ...]

all my objects have the same attibutes, but with different values.

Is there an easy way I can use a WHERE statement for that array?

Take the object where object.id = var

or do I just need to loop over the entire array and check every item? My array has over a 100 entries, so I wanted to know if there was a more efficient way

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use Array.find:

let array = [
    { id: 1, value: "itemname" },
    { id: 2, value: "itemname" }
];

let item1 = array.find(i => i.id === 1);

Array.find at MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

over 4 years ago · Santiago Trujillo Report

0

I'd use filter or reduce:

let array = [
    { id: 1, value: "itemname" },
    { id: 2, value: "itemname" }
];

let item1 = array.filter(item => item.id === 1)[0];
let item2 = array.reduce((prev, current) => prev || current.id === 1 ? current : null);

console.log(item1); // Object {id: 1, value: "itemname"}
console.log(item2); // Object {id: 1, value: "itemname"}

(code in playground)

If you care about iterating over the entire array then use some:

let item;
array.some(i => {
    if (i.id === 1) {
        item = i;
        return true;
    }
    return false;
});

(code in playground)

over 4 years ago · Santiago Trujillo Report

0

You can search a certain value in array of objects using TypeScript dynamically if you need to search the value from all fields of the object without specifying column

 var searchText = 'first';

let items = [
            { id: 1, name: "first", grade: "A" },
            { id: 2, name: "second", grade: "B" }
        ];

This below code will search for the value

var result = items.filter(item => 
             Object.keys(item).some(k => item[k] != null && 
             item[k].toString().toLowerCase()
             .includes(searchText.toLowerCase()))
             );

Same approach can be used to make a Search Filter Pipe in angularjs 4 using TypeScript

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!