Skip to content Skip to sidebar Skip to footer

Find A Value In A Javascript Object

I have a javascript object which looks like this: tabAndFieldLink: { 'key1': ['value11', 'value12', 'value13', 'value14', 'value15'], 'key2': ['value21', 'value22', 'value2

Solution 1:

You should use for...in statement to iterates over the enumerable properties of object tabAndFieldLink and as you need key return it.

for (key in obj) {
    if (obj[key].indexOf(nameKey) !== -1) {
        return key;
    }
}

var tabAndFieldLink = {
  'key1': ['value11', 'value12', 'value13', 'value14', 'value15'],
  'key2': ['value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'],
  'key3': ['value31', 'value32', 'value33', 'value34'],
  'key4': ['value41', 'value42'],
}

//err contain the value i.e. value22var err = 'value22';
var resultObject = search(err, tabAndFieldLink);
console.log("The result is :", resultObject);

functionsearch(nameKey, obj) {
  for (key in obj) {
    if (obj[key].indexOf(nameKey) !== -1) {
      return key;
    }
  }
}

Solution 2:

There are many inbuilt functional behavior present in the Javascript, which can be used in cases like this.

Here is the code using arrays filter

var tabAndFieldLink =  {
    'key1': ['value11', 'value12', 'value13', 'value14', 'value15'],
    'key2': ['value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'],
    'key3': ['value31', 'value32', 'value33', 'value34'],
    'key4': ['value41', 'value42'],
}


functiongetKey(checkValue) {
    for (key in tabAndFieldLink) {
        var newArr = tabAndFieldLink[key].filter(function(value){
            return value == 'value22';
        });
        if(newArr.length > 0) {
            console.log("Found......", key);
            return key;
            }
    }
}

console.log(getKey('value22'));

Solution 3:

You could use find and Object Keys function

var data = {
    'key1': ['value11', 'value22', 'value13', 'value14', 'value15'],
    'key2': ['value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'],
    'key3': ['value31', 'value32', 'value33', 'value34'],
    'key4': ['value41', 'value42'],
}

functionfindValue(search){
var result = []
Object.keys(data).forEach(function(key){
 var r = data[key].find(function(value){ 
           return value === search 
        })
 r && result.push(key)
})
console.log(result)
}

findValue("value22")

Solution 4:

You could check with Array#some, if the value is in the array with a specific key, then return that key.

functiongetKey(object, value) {
    var key;
    Object.keys(object).some(function (k) {
        if (object[k].indexOf(value) !== -1) {
            key = k;
            returntrue;
        }
    });
    return key;
}

var object = { key1: ['value11', 'value12', 'value13', 'value14', 'value15'], key2: [ 'value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'], key3: ['value31', 'value32', 'value33', 'value34'], key4: ['value41', 'value42'] },
    find = 'value22',
    key = getKey(object, find);

console.log(key);

ES6 with Array#find and Array#includes

functiongetKey(object, value) {
    returnObject.keys(object).find(k => object[k].includes(value));
}


var object = { key1: ['value11', 'value12', 'value13', 'value14', 'value15'], key2: [ 'value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'], key3: ['value31', 'value32', 'value33', 'value34'], key4: ['value41', 'value42'] },
    find = 'value22',
    key = getKey(object, find);

console.log(key);

Solution 5:

The tabAndFieldLink structure is a Hash containing key value pairs. Each value is an Array.

Navigate the structure as per the snippet below.

var tabAndFieldLink = {
  'key1': ['value11', 'value12', 'value13', 'value14', 'value15'],
  'key2': ['value21', 'value22', 'value23', 'value24', 'value25', 'value26', 'value27'],
  'key3': ['value31', 'value32', 'value33', 'value34'],
  'key4': ['value41', 'value42'],
};

var resultObject = search("value22", tabAndFieldLink);
console.log("The result is :", resultObject);

functionsearch(nameKey, myHash) {
  for (var key in myHash) {
    if (myHash[key].indexOf(nameKey) !== -1) {
      return key;
    }
  }
}

Post a Comment for "Find A Value In A Javascript Object"