我有一个对象数组,如下所示
const input = [ {id : "1" , text: "ABC", value: "abc" }, {id : "2" , text: "DEF", value: "def" }, {id : "3" , text: "LMN", value: "lmn" } ]
当传递各自的输入时,上面的包装函数应该返回以下内容
const ids = ["1", "3"]
const values = ["def", "lmn"]
const texts = ["DEF", "LMN"]
getValues(input, ids, "text") // ["ABC", "LMN"]
getValues(input, values, "id") // ["2", "3"]
getValues(input, texts, "value") // ["def", "lmn"]
当传递ids
个值的组并且必填字段为text
时,它应该从输入数组中返回匹配的text
个值.当values
和id
作为必填字段传递时也是如此,它应该从输入返回所有匹配的ids
我试过的代码
function getValues(input1, input2, field){
const result = input1.map(({id, text, value}) => {
if(text === input2){
return input1[field];
}
});
return result;
}