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",},
4
  • function(field, value) { return jobs.find(job => job[field] === value; } Commented Jun 13, 2019 at 12:44
  • Possible duplicate of Find object by id in an array of JavaScript objects Commented Jun 13, 2019 at 12:44
  • @str not really... READ FIRST BEFORE MARKING QUESTIONS AS DUPLICATE Commented Jun 13, 2019 at 16:01
  • @Dennis Compare the answer you accepted to this one in the duplicate question. Unlike you, I did read. Commented Jun 13, 2019 at 19:58

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

How Can I alter this to return a particular value in the array e.g "5/2017" after passing "startDate" to the findObject
I don't follow, can you elaborate?
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'))

4 Comments

How Can I alter this to return a particular value in the array e.g "5/2017" after passing "startDate" to the findObject
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
In short how do I get a string instead of an array as a log ??
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]
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"));

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.