Javascript 如何在对象数组中查找值?

Javascript 如何在对象数组中查找值?,javascript,Javascript,我有这样一个对象数组: var data = [ {id:1,first_name:'John',last_name:'Doe',age:20,birth:'1992-01-12'}, {id:2,first_name:'Blaan',last_name:'Adey',age:35,birth:'2001-04-16'} ]; var searchColumn = ['first_name','last_name','birth']; 这些值(搜索列)​​可能会改变,而不是恒定不

我有这样一个对象数组:

var data = [
   {id:1,first_name:'John',last_name:'Doe',age:20,birth:'1992-01-12'},
   {id:2,first_name:'Blaan',last_name:'Adey',age:35,birth:'2001-04-16'}
];

var searchColumn = ['first_name','last_name','birth'];
这些值(搜索列)​​可能会改变,而不是恒定不变。 因此,必须从数组中读取它们

我想现在就这样做:

function searchData(Val){
    var newData = [];
    if (Val){
        for (let i=0 ; i < data.length ; i++){
            searchColumn.forEach(value => {
                 if (data[i][value].includes(Val)){
                     newData.push(data[i]);
                 }
            }
        }
    }
}

searchData('L');
函数搜索数据(Val){
var newData=[];
if(Val){
for(设i=0;i{
if(数据[i][value]。包括(Val)){
newData.push(数据[i]);
}
}
}
}
}
searchData(“L”);
请引导我。

这样就可以了

您不必在数组中循环,您可以使用内置函数,如
map
some
filter
来实现这一点

var data = [
   {id:1,first_name:'John',last_name:'Doe',age:20,birth:'1992-01-12'},
   {id:1,first_name:'John',last_name:'Bal',age:20,birth:'1992-01-12'},
   {id:2,first_name:'Blaan',last_name:'Adey',age:35,birth:'2001-04-16'}
];

var searchColumn = ['first_name','last_name','birth'];

var search = (searchParam) => {
    // map here applies the function  (value) => searchColumn.some(property => value[property] === searchParam) ? value : null
    // over every value in the array & some loops through every value in the search column
    // looking for an object where one of the properties specified in searchColumn has a value that matches our search parameter
    return data.map(value => searchColumn.some(property => value[property] === searchParam) ? value : null)
        .filter(result => !!result); // here we use filter to remove the null values mapped in the map function
}

search('John');
// result -> [{id:1,first_name:'John',last_name:'Doe',age:20,birth:'1992-01-12'},
//         -> {id:1,first_name:'John',last_name:'Bal',age:20,birth:'1992-01-12'}]
更新:根据建议,更好的解决方案是:

var search = (searchParam) => {
    return data.filter(value => !!searchColumn.some(property => value[property] === searchParam));
}

…一种基于和的可配置、因而更通用的方法

函数doesItemMatchBoundPropertyValueAndSearch(项){
const{keyList,search}=this;
返回keyList.some(key=>item[key].includes(search));
}
常量sampleDataList=[
{id:1,名字:“约翰”,姓:“多伊”,年龄:20岁,出生:'1992-01-12'},
{id:2,名字:'Blaan',姓:'Adey',年龄:35岁,出生:'2001-04-16'},
];
const searchColumn=['姓','姓','出生'];
console.log(
“搜索:'L':”,
sampleDataList.filter(
doesItemMatchBoundPropertyValueAndSearch.bind({
关键字列表:searchColumn,
搜索:“L”,
})
)
);
console.log(
“搜索:'o':”,
sampleDataList.filter(
doesItemMatchBoundPropertyValueAndSearch.bind({
关键字列表:searchColumn,
搜索:“o”,
})
)
);
console.log(
“搜索:'n':”,
sampleDataList.filter(
doesItemMatchBoundPropertyValueAndSearch.bind({
关键字列表:searchColumn,
搜索:“n”,
})
)
);

.as控制台包装{最小高度:100%!重要;顶部:0;}
如果你想得到它的值,你必须在
searchData
的末尾返回
newData
。您好。我认为这个答案可以解决你的问题,请参阅:如果你纠正错误,你的代码会工作得很好。正如Matt指出的,既然你在函数中创建了
newData
,你就需要返回它来使用它。此外为此,缺少一个
,它应该关闭
.forEach(
。就在第二次关闭
}
之后。您还将返回整个数据数组项…
数据[i]
…对于包含搜索值的每个项目的属性,都是如此。但只要搜索值匹配,就只需返回此类项目一次。您可以
reduce()
而不是
map().filter())
并保存您自己的第二次迭代。@pilchard是的,很好的捕获,我对js lol有点生疏。@pilchard…不仅仅是一个简单的外部
过滤器
和内部
一些
是不需要的。@PeterSeliger函数
一些
是解决方案灵活所必需的,基本上这样做可以让您过滤在过滤数组时,不必手动检查数组中的任意多个属性(如
过滤器(v=>v[p]==s | | v[c]==r……)
),最好在某个地方定义它们,然后遍历该数组。@Bargros…1/2…你为什么要向我解释显而易见的事情?我从来没有说过什么不同的东西。我也正接近pilchard在他的评论中发表评论。但是,当然,既然我们现在正在讨论……你的方法可以改进为一个简单的
过滤器
函数在哪些功能上进行了
一些测试。首先不需要额外的
映射。这正是我在对皮尔查德的评论中已经说明的。。。