简体   繁体   中英

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...

 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 :

 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. Here I have also used destructuring assignment to get the value ( v ) of the current object from the passed in key . 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.

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')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM