TypeScript - Take object out of array based on attribute value

后端 未结 5 1834
名媛妹妹
名媛妹妹 2020-12-29 00:54

My array looks like this:

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

all my objects have the s

5条回答
  •  无人及你
    2020-12-29 01:18

    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

提交回复
热议问题