3

I have a list of the following sort:

[
    {
        "_name": "FIX.4.0"
    },
    {
        "_name": "FIX.4.1"
    },
    {
        "_name": "FIX.4.2"
    },
    {
        "_name": "FIX.4.3"
    },
    {
        "_name": "FIX.4.4"
    }
]

I want to check if "FIX.4.3" is present in the "_name" property of any of these objects or not. I can't change the format of the list (No matter how much I want to) because I fetch it from somewhere else. I know I can loop through the objects, but I'm looking for a better way. Thanks in advance.

3 Answers3

3

You can use some:

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

Example:

const data = [
        {
            "_name": "FIX.4.0"
        },
        {
            "_name": "FIX.4.1"
        },
        {
            "_name": "FIX.4.2"
        },
        {
            "_name": "FIX.4.3"
        },
        {
            "_name": "FIX.4.4"
        }
    ];
    
    // FIX.4.4 is there
    console.log(data.some(x => x._name === "FIX.4.4"));
    
    // FIX.NOT.THERE is, well, not
    console.log(data.some(x => x._name === "FIX.NOT.THERE"));
sloth
  • 99,095
  • 21
  • 171
  • 219
0

you can use find :

checkValue(value){
    if(this.array.find(function(obj) { return obj._name === value; }))
      return true
    else
      return false;
}
Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48
0

In the same spirit as sloth, but using find like fateme fazli

Exemple

const data = [
    {
        "_name": "FIX.4.0"
    },
    {
        "_name": "FIX.4.1"
    },
    {
        "_name": "FIX.4.2"
    },
    {
        "_name": "FIX.4.3"
    },
    {
        "_name": "FIX.4.4"
    }
];

const result = (data.find(x => x._name === "FIX.4.3") === undefined) ? false : true;

console.log(result);
i.G.
  • 77
  • 1
  • 4