[Vuejs]-Filter array present inside a array of objects without affecting the main array

1๐Ÿ‘

โœ…

You can do it with a map and a filter, to avoid to modify the original array, you can use Object.asign

var employee = {
  "value": [{
      "position": "Seniro Developer",
      "description": "Developemwnt",
      "workers": [{
          "name": "Kumar",
          "age": 22
        },
        {
          "name": "aravinth",
          "age": 29
        },
        {
          "name": "sathish",
          "age": 35
        }
      ]
    },
    {
      "position": "Tester",
      "description": "testing",
      "workers": [{
          "name": "vinth",
          "age": 18
        },
        {
          "name": "rahul",
          "age": 45
        },
        {
          "name": "sathish",
          "age": 12
        }
      ]
    }
  ]
}



var filteredResult = employee.value.map(e => {
  let filter = e.workers.filter(w => w.age < 25)
  
  return Object.assign({}, e, {workers: filter})
})

console.log('original', employee)
console.log('result', filteredResult)

1๐Ÿ‘

You could reduce the array and check if the filtered workers have some elements then push a new object with changed workers to the result set.

var employee = { value: [{ position: "Seniro Developer", description: "Developemwnt", workers: [{ name: "Kumar", age: 22 }, { name: "aravinth", age: 29 }, { name: "sathish", age: 35 }] }, { position: "Tester", description: "testing", workers: [{ name: "vinth", age: 18 }, { name: "rahul", age: 45 }, { name: "sathish", age: 12 }] }] },
    value = employee.value.reduce((r, o) => {
        const workers = o.workers.filter(({ age }) => age < 25);
        if (workers.length) r.push({ ...o, workers });
        return r;
    }, []),
    result = { value };

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
๐Ÿ‘คNina Scholz

1๐Ÿ‘

You can also try this:

var employee = { "value": [ { "position": "Seniro Developer", "description": "Developemwnt", "workers": [ { "name": "Kumar", "age": 22 }, { "name": "aravinth", "age": 29 }, { "name": "sathish", "age": 35 } ] }, { "position": "Tester", "description": "testing", "workers": [ { "name": "vinth", "age": 18 }, { "name": "rahul", "age": 45 }, { "name": "sathish", "age": 12 } ] } ]}
result = employee.value.map(({workers, ...rest})=>({...rest, workers:[...workers.filter(k=>k.age<25)]}));

console.log(result);
๐Ÿ‘คgorak

0๐Ÿ‘

Use map and while creating the workers key in return object use filter to get employee with age less than 25. map will create an array

var employee = {
  "value": [{
      "position": "Seniro Developer",
      "description": "Developemwnt",
      "workers": [{
          "name": "Kumar",
          "age": 22
        },
        {
          "name": "aravinth",
          "age": 29
        },
        {
          "name": "sathish",
          "age": 35
        }
      ]
    },
    {
      "position": "Tester",
      "description": "testing",
      "workers": [{
          "name": "vinth",
          "age": 18
        },
        {
          "name": "rahul",
          "age": 45
        },
        {
          "name": "sathish",
          "age": 12
        }
      ]
    }
  ]
}

let filteredEmployee = employee.value.map((item) => {
  return {
    "position": item.position,
    "description": item.description,
    "workers": item.workers.filter(elem => elem.age < 25)
  }
});
let newObject = Object.assign({}, {
  value: filteredEmployee
});

console.log(newObject)
๐Ÿ‘คbrk

0๐Ÿ‘

You can use map method with ... rest syntax:

employee.value.map(({workers, ...rest}) => ({...rest, 
    workers: workers.filter(w => w.age < 25)}));

An example:

let employee = {
    "value": [
      {
        "position": "Seniro Developer",
        "description": "Developemwnt",
        "workers": [
          {
            "name": "Kumar",
            "age": 22
          },
          {
            "name": "aravinth",
            "age": 29
          },
          {
            "name": "sathish",
            "age": 35
          }
        ]
      },
      {
        "position": "Tester",
        "description": "testing",
        "workers": [
          {
            "name": "vinth",
            "age": 18
          },
          {
            "name": "rahul",
            "age": 45
          },
          {
            "name": "sathish",
            "age": 12
          }
        ]
      }
    ]
  }

  const result = employee.value.map(({workers, ...rest}) => ({...rest, workers: workers.filter(w => w.age < 25)}));
  console.log(result);
๐Ÿ‘คStepUp

Leave a comment