0

I'm working on a Vue project and I'm just new to Vue JS, my question is how can I return all data that has an id of 1 when I input a value. below is a sample structure of my data.

 {
    'A': [{
        1: [{
            id: 1, 
            name: 'John'
        }],
        2: [{
            id: 5,
            name: 'Ken'
        }]
    }],
    'B': [{
        1: {
            id: 1,
            name: 'Leena'
        }
    }],
    'C': [{
        1: [{
            id: 1,
            name: 'Jesica'
        }],
        2: [{
            id: 18,
            name: 'Mike'
        }]
    }]
}

Expected result should be (below), since they all have same id value

{
    'A': [{
        1: [{
            id: 1, 
            name: 'John'
        }]
    }],
    'B': [{
        1: {
            id: 1,
            name: 'Leena'
        }
    }],
    'C': [{
        1: [{
            id: 1,
            name: 'Jesica'
        }]
    }]
}
6
  • This is a weird data structure. Will each object property always contain an array containing only a single object? Commented May 23, 2017 at 22:30
  • no it's just an example, it could return multiple arrays as long as the input value is equal to field value that I want to search Commented May 23, 2017 at 22:35
  • So, say your object is named 'foo'. You mean foo['A'][0][1] could be an array of many objects, and you want any of those objects with id==1 returned in the filtered array? Commented May 23, 2017 at 22:38
  • exactly @thanksd, here is what I'm trying to do on my app jsbin.com/dezokiwowu/edit?html,js,console,output Commented May 23, 2017 at 22:43
  • Do you just need to filter an array of objects by an object property name? That's what it seems like in your app, which doesn't use this crazy data structure. Commented May 23, 2017 at 22:47

1 Answer 1

3

If you have any control over the format of the data structure, I would change it to something more manageable. For example, setting each capital letter property to an array of objects:

let dataset = { 
  'A': [{id: 1, name: 'John'}, {id: 2, name: 'Sally'}],
  'B': [{id: 1, name: 'Bob'}],
  ...
}

Even better would be to make the data structure an array of objects with a capital letter as a group property value and the id-name objects as people property values (or whatever would make sense to you):

let dataset = [{
    group: 'A', people: [{id: 1, name: 'John'}, {id: 2, name: 'Sally'}],
  }, {
    group: 'B', people: [{id: 1, name: 'Bob'}],
  }, {
    ...
}]

Using this structure, it would be relatively simple to filter the data by a specific id:

function filterObject(foo, id) {
  return foo.map(i => i.people.filter(j => j.id == id));
}

Here's a working fiddle.


If you really need to use the crazy data structure you've provided, here's a function that will do what you need:

let filterObject = function(foo, id) {
  let bar = {};

  Object.keys(foo).map((key) => {
    for (let i = 0; i < foo[key].length; i++) {
      Object.keys(foo[key][i]).map((index) => {
        for (let j = 0; j < foo[key][i][index].length; j++) {
          if (foo[key][i][index][j].id != id) {
            continue;
          }

          if (!bar[key]) {
            bar[key] = [];
          }
          if (!bar[key][i]) {
            bar[key][i] = {};
          }
          if (!bar[key][i][index]) {
            bar[key][i][index] = [];
          }

          bar[key][i][index][j] = foo[key][i][index][j];
        }
      })
    }
  });

  return bar
}

Here's a working fiddle.

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

1 Comment

@PenAndPapers see my updated answer for a way to change your data structure to make the filtering function much simpler

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.