0

How to search for an element in a javascript array using find?

 const array = [{
    "key1": "val1",
    "key2": "val2",
    "key3": "val3",
    "key4": "val4",
    "key5": "val5",
},
{
    "key1": "val6",
    "key2": "val7",
    "key3": "val8",
    "key4": "val9",
    "key5": "val10"
}]

I want to search for value searchvalue. I assume it will match val1 and if so, I want to get key5:

array.find((element) => element.key1 === searchvalue).key5)

But if there is no matching element.key1 === searchvalue, then I want to search key2 and find key6 of the result:

array.find((element) => element.key2 === searchvalue).key5)

As of now what I do is:

var arrayElement = array.find((element) => element.key1 === 
    searchvalue)
const value =  (arrayElement !== undefined ) ? arrayElement.key5: array.find((element) => element.key2 === 
    searchvalue).key6;
1
  • 1
    corrected the question Commented Sep 17, 2019 at 7:26

3 Answers 3

1

You could chain the attempts to find an object and get the value from it.

var array = [{ key1: "val1", key2: "val2", key3: "val3", key4: "val4", key5: "val5" }, { key1: "val6", key2: "val7", key3: "val8", key4: "val9", key5: "val10" }],
    searchvalue = "val7",
    result = 
        (array.find(o => o.key1 === searchvalue) || {}).key5 ||
        (array.find(o => o.key2 === searchvalue) || {}).key4;

console.log(result);

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

2 Comments

sorry, have edited the question, if the initial search is not successful, then search criteria and result key changes
as long as the value is truthy, you could chain the findings and take an object as default object.
1

Your data is array of object. So you can make another iteration over the object keys using .some() to find the match

const array = [
    {"key1": "val1","key2": "val2","key3": "val3","key4": "val4","key5": "val5",},
    {"key1": "val6","key2": "val7","key3": "val8","key4": "val9","key5": "val10",}]

const search = 'val6';
const res = (array.find(i => Object.keys(i).some(k => search === i[k])) || {}).key5
console.log(res)

1 Comment

sorry, have edited the question, if the initial search is not successful, then search criteria and result key changes
1

You can loop over values of each element in array and match them with search value if found return that element or else use an empty object as default value, later access key5 on found value

const array = [{"key1": "val1","key2": "val2","key3": "val3","key4": "val4","key5": "val5",},{"key1": "val6","key2": "val7","key3": "val8","key4": "val9","key5": "val10",}]

let find = (arr,searchValue) => (arr.find(v => {
  return Object.values(v).some(value => value === searchValue)
}) || {}).key5

console.log(find(array,'val3'))
console.log(find(array,'no matching value'))

1 Comment

have edited the question, if the initial search is not successful, then search criteria and result key changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.