0

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",},
chelmertz
  • 20,399
  • 5
  • 40
  • 46
Shei
  • 393
  • 4
  • 15
  • `function(field, value) { return jobs.find(job => job[field] === value; }` – Roberto Zvjerković Jun 13 '19 at 12:44
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – str Jun 13 '19 at 12:44
  • @str not really... READ FIRST BEFORE MARKING QUESTIONS AS DUPLICATE – Shei Jun 13 '19 at 16:01
  • @Dennis Compare the answer you accepted to [this one](https://stackoverflow.com/a/53737342/906113) in the duplicate question. Unlike you, I *did* read. – str Jun 13 '19 at 19:58

4 Answers4

1

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);
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
1

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'))
Kobe
  • 6,226
  • 1
  • 14
  • 35
  • How Can I alter this to return a particular value in the array e.g ``"5/2017"`` after passing ``"startDate"`` to the `findObject` – Shei Jun 13 '19 at 13:34
  • I'have tried playing around the data and replaced the `startDate` values with `null` and I get `[null]` as a log... How do I perform a check to check if it's actually `null` – Shei Jun 13 '19 at 13:53
  • In short how do I get a string instead of an array as a log ?? – Shei Jun 13 '19 at 13:55
  • The issue is that you are using filter, which will return an array of objects, and map will return an array of strings. If you only wanted to log each string, you could use the index, like `[0]` – Kobe Jun 13 '19 at 14:04
0

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"));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

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'));
Manish Khedekar
  • 392
  • 3
  • 13