![](/img/trans.png)
[英]How to add second array objects in first array object based on id value using angular6?
[英]How to filter the array of objects based on array of strings by using angular6
我有 2 個字符串數組和 1 個對象數組。 我需要根據 2 個字符串數組進行過濾。 我怎樣才能做到這一點?
let profissionals = [
{state:'ap',type:'maths',price:200},
{state:'ap',type:'english',price:400},
{state:'ka',type:'social',price:200},
{state:'ts',type:'english',price:200},
{state:'ap',type:'maths',price:500},
{state:'ka',type:'maths',price:600},
{state:'ts',type:'english',price:200},
{state:'kl',type:'english',price:100},
{state:'kl',type:'english',price:300},
{state:'ap',type:'social',price:200},
{state:'gj',type:'english',price:600},
{state:'kl',type:'social',price:600}
]
let typeList=['maths','social'];
let stateList=['ap','ka'];
輸出應該是這樣的
fliteredlist = [
{state:'ap',type:'maths',price:200},
{state:'ap',type:'maths',price:500},
{state:'ka',type:'maths',price:600},
{state:'ap',type:'social',price:200},
{state:'ka',type:'social',price:200},
]
您可以使用Array.filter()
:
let profissionals = [ {state:'ap',type:'maths',price:200}, {state:'ap',type:'english',price:400}, {state:'ka',type:'social',price:200}, {state:'ts',type:'english',price:200}, {state:'ap',type:'maths',price:500}, {state:'ka',type:'maths',price:600}, {state:'ts',type:'english',price:200}, {state:'kl',type:'english',price:100}, {state:'kl',type:'english',price:300}, {state:'ap',type:'social',price:200}, {state:'gj',type:'english',price:600}, {state:'kl',type:'social',price:600} ] let typeList=['maths','social']; let stateList=['ap','ka']; let output = profissionals.filter(({type, state}) => (typeList.length === 0 || typeList.includes(type)) && (stateList.length === 0 || stateList.includes(state))); console.log(output);
profissionals.filter(({state, type}) => stateList.includes(state) && typeList.includes(type));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.