1

I am not being able to get the String 'ola' from "label" while searching for the "value" 174.

In a similar question I found a piece of code similar from what I have: How to get a key in a JavaScript object by its value? . I tried it, but it returned undefined. Since then I have been changing it to try to return something. Right now as it is it returns a boolean value (false).

a = {
    list: {0: {label: 'ola', value: 174}}
};
value = 174;

console.log([Object.values(a.list).includes(value)]);

I would like to return just the String 'ola' by searching for the "value" it has, in this case it is 174. And I would like to keep the structure of the Object "a" as it is.

fribeiroc
  • 19
  • 8
  • 3
    variable `a` is not an array, it's an object. –  Feb 04 '19 at 13:53
  • You should try to use `Object.keys` like here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys Or some other `Object` methods – Guillaume Munsch Feb 04 '19 at 13:57

6 Answers6

1

It was said before, but list is not an Array, perhaps something like this would help you achieve what you want ?

const a = {
  list: {
      0: {
        label: 'ola', 
        value: 174
      }
  }
};
const value = 174;

function find(obj, val) {
  let res = null;
  Object.keys(a.list).forEach((k) => {
    if (a.list[k].value === val) {
      res = a.list[k].label;
    }
  });
  return res;
}

console.log(find(a, value));
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
  • I tried this code and it worked. I the first one didn't worked, it always gave me undefined. The codes below I didn't try, so maybe they also work! – fribeiroc Feb 04 '19 at 16:54
0

Use Object#values and Array#find and destructuring.

Array#find will return undefined if the value you're looking for doesn't exist. Use the || operator to handle that specific case before accesing the label property of what was found.

const a = {
    list: {0: {label: 'ola', value: 174}}
};
const value = 174;

function find(v){
  return (Object.values(a.list).find(({value})=>value===v)||{}).label;
}

const res = find(value);

console.log(res);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
0

a.list is an object, use for in to iterate through its values.

a = {
    list: {0: {label: 'ola', value: 174}}
};
value = 174;

for (const key in a.list) {
    if (a.list.hasOwnProperty(key)) {
        const element = a.list[key];
        console.log(element.value === value)
    }
}
Ryan Searle
  • 1,597
  • 1
  • 19
  • 30
0

You need to find the key WITHIN the list.

var a = {
  list : {
    0 : {
      label: 'ola',
      value: 174
    }
  }
},
value = 174;

console.log(findKey(a.list, value, 'label', 'value'));

function findKey(obj, value, labelField, valueField) {
  var foundKey = Object.keys(obj).find(x => obj[x][valueField] === value);
  return foundKey != null ? obj[foundKey][labelField] : null;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Array.prototype.includes() will not work on objects. If possible, I would flip the key and value, it makes more sense to search for a key than a value because you can't have duplicating keys in an object.

However, it seems like from the code snippet that you wanted "list" to be an array and not an object.

Here's the proper json:

a = {
    list: [
        {label: 'ola', value: 174}
    ]
}
0

First function returns a string of the label.
Second function returns an array with all labels that matches.

function findLabelByValue(value){ // Returns first matching label found
    return Object.values(a.list).find(element => element.value == value).label;
}

function findLabelsByValue(value){ // Returns all matching labels as array
    return Object.values(a.list).reduce(function(list, element){
        if(element.value == value)
            return list.push(element.label);
        return list;
    }, []);
}

In use of:
Object.values(), Array.find(), Array.reduce()