Shei
Shei

Reputation: 483

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

nameOfFunction(jobs,"seniority","Senior")

and it returns/logs

{"startDate": "5/2017","endDate": null,"isCurrent": true,"seniority": "Senior",},

Upvotes: 0

Views: 89

Answers (4)

Kobe
Kobe

Reputation: 6446

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

Upvotes: 1

Manish Khedekar
Manish Khedekar

Reputation: 402

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

Upvotes: 0

Nick Parsons
Nick Parsons

Reputation: 50684

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

Upvotes: 0

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

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

Upvotes: 1

Related Questions