0

I am having trouble assigning a value from my nested array. The loop is moving through correctly and looks to initially make a comparison of the following arrays.

let searchSource = [ 'media', 'arts'];

let subscriberNodes = [
   {
      name: 'customer',
      nodes: [
         {source: 'media'},
      ]
   }
]

Unfortunately with the code below I am not filtering through the data.

 let violation = subscriberNodes
      .filter( v => v.nodes = v.nodes.filter(q => searchSource === q.source) );

The searchName and q.source compare on one phase of the break point in the debugger but as I click again

 q.source // 'EDW'  to undefined

I am not sure why I am seeing it compare the same values 2 times and why the second time q.source is undefined. It seems like filters are running 2 times.

I tried this with find as well but errored out the same result on the filter

  let qualifierViolations = profilerNodes
        .find(item => item.nodes)
        .filter(value => value.source === smartSearch)

When using .some I found that everything was getting touched but nothing filtered

let qualifierViolations = subscriberNodes
        .filter( v => v.nodes.some(q => searchSource !== q.source) );

At this point I do not know what I am missing or how to fix it. Any insight would be greatly appreciated.

let subscriberNodes = [ { name: 'customer', nodes: [ {source: 'media'}, ] } ]

1
  • You talk about four different variables, two are never given any value in your code. I think there are just two relevant ones: one filter array, and one array to filter. Remove the rest or give additional info Commented Jul 14, 2017 at 10:15

1 Answer 1

1

I'm not sure of what you want to filter out, but if you want to keep only the users that have {source: 'media'} or {source: 'arts'}, here's how you can do it :

let searchSouce = ['media', 'arts'];

let subscriberNodes = [{
    name: 'customer1',
    nodes: [{
      source: 'media'
    }]
  },
  {
    name: 'customer2',
    nodes: [{
      source: 'arts'
    }, {
      source: 'something'
    }]
  },
  {
    name: 'customer3',
    nodes: [{
      source: 'something else'
    }]
  }
]

let violation = subscriberNodes.filter(subscriber =>
  subscriber.nodes.filter(node => searchSouce.indexOf(node.source) !== -1).length
);


console.log(violation) // Keeps only customer 1 and 2

nodes.filter will keep the nodes that contain {source: 'media'} or {source: 'arts'}. So, getting the filtered .length will return 0 or greater, which will in turn decide whether the subscriber is eventually kept or not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.