1

I have an array of objects of the following form:

jsonParseEntityVariables = [
{"id":0, "text": []},
{"id":1, "text": []},
{"id":2, "text": []}
];

I also have a var with name entity_variables.currentId in which I store an ID

How can I compare all jsonParseEntityVariables.id with entity_variables.currentId to decide that entity_variables.currentId is not equal at all to any jsonParseEntityVariables.id?

hindmost
  • 7,125
  • 3
  • 27
  • 39
  • 1
    JSON is a *textual notation* for data exchange. [(More.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. (You may -- or may not -- have *started* with JSON, but by the time you're doing the above, you're not dealing with JSON anymore and it's irrelevant to the question.) – T.J. Crowder Mar 21 '17 at 09:38

4 Answers4

0

You can use either Array#every. If none of the ids in the json are equal to given value, it will return true.

var jsonParseEntityVariables = [{"id":0, "text": []},{"id":1, "text": []},{"id":2, "text": []}],
    currentId = 4;

    console.log(jsonParseEntityVariables.every(v => v.id != currentId));

or Array#some, that will return false if there are no equal values. If there's even one matched element, it will return true.

var jsonParseEntityVariables = [{"id":0, "text": []},{"id":1, "text": []},{"id":2, "text": []}],
    currentId = 4;

    console.log(jsonParseEntityVariables.some(v => v.id == currentId));
kind user
  • 40,029
  • 7
  • 67
  • 77
0

What you have is an array of objects. You can readily check if their id values do, or don't, matcy entity_variables.currentId with Array#some:

if (jsonParseEntitiesVariables.some(function(e) { return e.id === entity_variables.currentId;})) {
    // At least one matched
} else {
    // None matched
}

Or with an ES2015+ arrow function:

if (jsonParseEntitiesVariables.some(e => e.id === entity_variables.currentId)) {
    // At least one matched
} else {
    // None matched
}

If you want to know which entry matched, Array#find or findIndex would be what you're looking for:

var match = jsonParseEntitiesVariables.find(e => e.id === entity_variables.currentId);
if (match) {
    // Found it, `match` contains it
} else {
    // Not found
}

findIndex works the same way, but returns the index of the found entry (or -1 if not found).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

var jsonParseEntityVariables = [{"id":0, "text": []},{"id":1, "text": []},{"id":2, "text": []}], currentId = 1; console.log(jsonParseEntityVariables.every(jsonParseEntityVariables => jsonParseEntityVariables.id != currentId));

0

This code checks for duplicate id-s of your id.

var id_duplicate = false;
for (i in jsonParseEntityVariables){
    if (jsonParseEntityVariables[i].id === entity_variables.currentId){
        id_duplicate = true;
        break;
    }
}

Note! your jsonParseEntityVariables is a number. make sure that entity_variables.currentId is also a number.

Meltinglava
  • 179
  • 1
  • 11