[英]Return array object from a multi-dimensional array of objects that contains a specific Value
I'm trying to create a function that acts like a search mechanism which goes through an array of objects and returns a particular array object which contains a particular value (search parameter) in this array 我正在尝试创建一个功能类似于搜索机制的函数,该函数遍历对象数组并返回包含此数组中特定值(搜索参数)的特定数组对象
var jobs= [
{
"startDate": "5/2017",
"endDate": null,
"isCurrent": true,
"seniority": "Senior",
},
{
"startDate": "5/2013",
"endDate": "5/2019",
"isCurrent": false,
"seniority": "Junior",
},
]
I want to create a function where you provide an array, array key and an array value ie 我想创建一个函数,在其中提供数组,数组键和数组值,即
nameOfFunction(jobs,"seniority","Senior")
and it returns/logs 它返回/日志
{"startDate": "5/2017","endDate": null,"isCurrent": true,"seniority": "Senior",},
The array's filter method does this, but if you wanted to wrap it, you could do something like this... 数组的filter方法可以做到这一点,但是如果要包装它,则可以执行以下操作...
var jobs= [ { "startDate": "5/2017", "endDate": null, "isCurrent": true, "seniority": "Senior", }, { "startDate": "5/2013", "endDate": "5/2019", "isCurrent": false, "seniority": "Junior", }, ] const nameOfFunction = (ar, key, val) => ar.filter(obj=>obj[key]===val); var results = nameOfFunction(jobs,"seniority","Senior") console.log(results);
You can use filter
: 您可以使用
filter
:
var jobs= [ { "startDate": "5/2017", "endDate": null, "isCurrent": true, "seniority": "Senior", }, { "startDate": "5/2013", "endDate": "5/2019", "isCurrent": false, "seniority": "Junior", }, ] const findObject = (obj, prop, value) => obj.filter(obj => obj[prop] === value) console.log(findObject(jobs, 'seniority', 'Senior'))
EDIT: 编辑:
var jobs= [ { "startDate": "5/2017", "endDate": null, "isCurrent": true, "seniority": "Senior", }, { "startDate": "5/2013", "endDate": "5/2019", "isCurrent": false, "seniority": "Junior", }, ] const findObject = (obj, prop, value, key) => obj.filter(obj => obj[prop] === value).map(obj => obj[key]) console.log(findObject(jobs, 'seniority', 'Senior', 'startDate'))
You could use the filter
method on your passed in array. 您可以在传入的数组中使用
filter
方法。 Here I have also used destructuring assignment to get the value ( v
) of the current object from the passed in key
. 在这里,我还使用了分解分配,以从传入的
key
中获取当前对象的值( v
)。 I then compare the value of the object ( v
) with the val
passed into the function to see whether it should be kept in the new array. 然后,我将对象(
v
)的值与传递给函数的val
进行比较,以查看是否应将其保留在新数组中。
See example below: 请参见下面的示例:
const jobs= [ { "startDate": "5/2017", "endDate": null, "isCurrent": true, "seniority": "Senior", }, { "startDate": "5/2013", "endDate": "5/2019", "isCurrent": false, "seniority": "Junior", }, ]; const filterArr = (arr, key, val) => arr.filter(({[key]:v}) => v===val); console.log(filterArr(jobs, "seniority", "Senior"));
You may try out like, 您可以尝试一下,
var jobs= [ { "startDate": "5/2017", "endDate": null, "isCurrent": true, "seniority": "Senior", }, { "startDate": "5/2013", "endDate": "5/2019", "isCurrent": false, "seniority": "Junior", }, ]; // This function will return array of filtered elements function searchArray(array,propertyKey,propertyValue){ return array.filter(function(a){ return a[propertyKey] === propertyValue; }); } console.log(searchArray(jobs, 'seniority', 'Senior')); // With new way function searchArrayNewMethod(array,propertyKey,propertyValue){ return array.filter( a => a[propertyKey] === propertyValue); } console.log(searchArrayNewMethod(jobs, 'seniority', 'Senior'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.