-2

I have some obejct thats looks like this

{
  "tasks": [
    {
      "name": "Task YES",
      "finished": false,
      "id": 1
    },
    {
      "name": "Task NO",
      "finished": false,
      "id": 2
    }
  ]
}

And some value

let someValue = 'Task NO';

How to check if there already value inside JSON?

Bharata
  • 13,509
  • 6
  • 36
  • 50
Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142

5 Answers5

3

One way to accomplish what you're looking to do is to use Array.prototype.find().

let task = tasks.find(task => task.name === someValue);

If the task is found with the same name as someValue, task will be equal to the found task. If it can't find an anything, then task will be undefined.

const taskList = {
  "tasks": [
    {
      "name": "Task YES",
      "finished": false,
      "id": 1
    },
    {
      "name": "Task NO",
      "finished": false,
      "id": 2
    }
  ]
};

let someValue = 'Task NO';
let task = taskList.tasks.find(task => task.name === someValue);
console.log(task);

if (task) console.log(`Task with the ID of ${task.id} already has this name`);
else console.log("No task with that name was found");
Sam Holmes
  • 1,594
  • 13
  • 31
  • _"The best way..."_ totally depends on the use case and what the TO plans to do with the information that there might already be a task with a given name. The _"best way"_ could also be to use `Array.prototype.some()`, or `Array.prototype.filter()` or a simple `for` loop ... – Andreas Jul 15 '18 at 15:03
  • Sorry you're right, that's totally my bad on the phrasing. I've modified my answer with better wording :) – Sam Holmes Jul 15 '18 at 15:05
  • You could instantly cast it to a boolean like this: `let task = !!tasks.find(task => task.name === someValue);` – Get Off My Lawn Jul 15 '18 at 15:16
  • At that point you may as well use `Array.prototype.some()`, as you suggested in your answer. I thought the OP may want to know what the ID for the found task was, which requires preserving the found element. Without knowing the context I can't know either way. – Sam Holmes Jul 15 '18 at 15:17
1

This looks like a job for some, which tests whether at least one element in the array passes the test implemented by the provided function.

let obj = {
  "tasks": [
    {
      "name": "Task YES",
      "finished": false,
      "id": 1
    },
    {
      "name": "Task NO",
      "finished": false,
      "id": 2
    }
  ]
}

console.log(obj.tasks.some(i => i.name == 'Task NO'))
console.log(obj.tasks.some(i => i.name == 'Task YES'))
console.log(obj.tasks.some(i => i.name == 'Task ABC123'))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

Try the following function:

function isValueExist(obj, value) {
  if (!(obj instanceof Object) && 'function' !== typeof obj) {
    return obj === value;
  }

  for (var prop in obj)  {
    if (isValueExist(obj[prop], value)) {
      return true;
    }
  }

  return false;
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
0

The JSON.parse reviver parameter can be used to recursively enumerate, modify, or exclude values:

var someValue = 'Task NO', json = '{"tasks":[{"name":"Task YES","finished":false,"id":1},{"name":"Task NO","finished":false,"id":2}]}';

var obj = JSON.parse(json, function(key, value) {
  if (value == someValue) 
    console.log(`found: '${value}' at key '${key}'`);
  return value;                                           // optional if the result obj is needed
});
Slai
  • 22,144
  • 5
  • 45
  • 53
-1

quite inefficient but a hacky version with Boolean object -))

Boolean(d.tasks.filter(e => e.name == "Task NO"))

EDIT

saved 7 bytes -)) thanks to @Get Off My Lawn

!!d.tasks.filter(e => e.name == "Task NO")

Another Update.

As @Andreas commented, Boolean([]) returns true. So, answer should be updated as,

!!d.tasks.filter(e => e.name == "Task NO").length
marmeladze
  • 6,468
  • 3
  • 24
  • 45