JavaScript How to filter array of objects and get filtered object value filter array objects javascript,filter array objects angular 6,filter

I have an array model as below:

nodes:[
 { id: 1, label: 'label1'},
 { id: 2, label: 'label2'},
 { id: 3, label: 'label3'}
]

I whant to get the label of node filtering by id

I tried the next way, but dont get it work

const selectedNode = 2;

const nodeLabel = nodes.filter(({id}) => id.label ? id === selectedNode) // its filter here
Answer:1

You can use find method by passing a callback provided function.

The find() method returns the value of the first element in the array that passed the provided testing function. Otherwise undefined is returned.

let nodes=[
 { id: 1, label: 'label1'},
 { id: 2, label: 'label2'},
 { id: 3, label: 'label3'}
];
let id=2;
let node = nodes.find(a=>a.id == id);
console.log(node ? node.label : 'id not found');
Answer:2

nodes.find(node => node.id === selectedNode).label

Answer:3

You were quite close.

This line

nodes.filer(({id}) => id.label ? id === selectedNode)

has few issues (assuming filer was just a typo)

  • It is comparing an integer with an object. (id is the object here)

  • filter will give you the list of objects rather than its property label.

  • You were comparing label with id value.

  • {id} to be replaced by id.

Just modify this to

nodes.filter( (id) => id.id === selectedNode )[0].label

Demo

var nodes = [
 { id: 1, label: 'label1'},
 { id: 2, label: 'label2'},
 { id: 3, label: 'label3'}
];
var selectedNode = 2;
console.log( nodes.filter( (id) => id.id === selectedNode )[0].label );
Answer:4

I am trying to download inside div content in text format. I can able to download inside div content in txt format but all the contents are same line . I need to add the line break in each element. ...

I am trying to download inside div content in text format. I can able to download inside div content in txt format but all the contents are same line . I need to add the line break in each element. ...

Assume we have a simple front end and (let's assume Angular if it matters) and a back end app. Say the front end app does a get request. Usually the angular repository makes an $http.get request which ...

Assume we have a simple front end and (let's assume Angular if it matters) and a back end app. Say the front end app does a get request. Usually the angular repository makes an $http.get request which ...

How do I get the size (in bytes) of a bufferd image in javascript? I am not allowed to trust the file size in the client side, and need to verify in the backend as part of upload validation. My setup ...

How do I get the size (in bytes) of a bufferd image in javascript? I am not allowed to trust the file size in the client side, and need to verify in the backend as part of upload validation. My setup ...

  1. byte buffer size
  2. java bytebuffer size
  3. byte buffer size golang
  4. new byte buffer_size
  5. byte array buffer size
  6. byte buffer size c#

There is a vuejs component called my-comp with a given data that I want to iterate on its elements and print them: <my-comp json-data='[{"id":1,"text":"Hello"},{"id":2,"text":"World"}]' inline-...

There is a vuejs component called my-comp with a given data that I want to iterate on its elements and print them: <my-comp json-data='[{"id":1,"text":"Hello"},{"id":2,"text":"World"}]' inline-...