0

Learning by coding, here i have an array of objects (data), and nodeId which is number , i want to check if that arrays 'target' has same value as nodeId then 'return', should i use map(), find(), filter(), how should i know which to use ? english is not my mother language so could be mistakes

data:

  const Test = '7'
const nodeId = parseInt(Test);

  
  const data = [
    { target: 4, name: "usa" },
    { target: 7, name: "England" },
    { target: 3, name: "Japan" }
  ];
  
  
   if (check if data and nodeId both have same value then) {
    return;
  }
walee
  • 575
  • 4
  • 16
  • any help is appreciated – walee May 22 '22 at 20:52
  • *"then 'return'"*: and else? Also, `return` is only valid inside a function... And if you `return`, you might want to pass some *result* to the caller of that function (what should the result be?) – trincot May 22 '22 at 20:53
  • What are you returning (from your imaginary function)? Is it the object in the array that matches the `nodeId`, or do you just want to return `true` if it finds a match? – Andy May 22 '22 at 20:54
  • if it find a match could return true or false – walee May 22 '22 at 20:56
  • do you mind at least trying it first? you can read the docs on [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map), [`.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find), and [`.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to see which one to use. Then if something is wrong we can help debug – about14sheep May 22 '22 at 20:56
  • 1
    [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) is the method you need. It will shortcut when it finds a match and returns a boolean. `return arr.some(obj => obj.target === nodeId)`. – Andy May 22 '22 at 20:58
  • 1
    Please [edit] your question to show what research you have done into the problem and any attempts you've made based on that research. – Heretic Monkey May 22 '22 at 20:59
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Heretic Monkey May 22 '22 at 21:07

2 Answers2

0

.some() is probably what you want to use

const check = data.some((elem) => elem.target === nodeId) 
console.log(check) // true if exist or false if it doesnt
if(check) {
  // reached if it exist
}

check would be false if it doesn't exist or true if it does. It will stop running once it finds a match whereas .filter() and .map() would run the entire array. If you also want to get the element then you should use .find() instead.

Bas
  • 1,353
  • 3
  • 7
  • 18
0

Your question is a little vague but i'm going to try and answer it as easily as possible.

should i use map(), find(), filter(), how should i know which to use ?

That depends on what your hypothetical function or code would require, for example if you want the item that matched your value, then you would use .find() because it returns single value based on a condition.

similarly if you wish to return an array based on items that match the conditions, you would use .filter(), and same if you just want to check if your value exists in the array, you could use .some or .every.

in your code example, I'm assuming you want to return the specific value that matched with your nodeId, with that assumption, here's how that will work

function getDataBasedOnNodeId (dataObjects, matchValue) { 
 return dataObjects.find(item => item.target === Number(matchValue))
}

const data = [
    { target: 4, name: "usa" },
    { target: 7, name: "England" },
    { target: 3, name: "Japan" }
];
  

return getDataBasedOnNodeId(data, 7)
Umair Ahmed
  • 542
  • 5
  • 12