Filter data inside array object of array object using javascript

javascript filter array of objects by another array of objects
javascript filter array of objects by key
filter array of objects javascript
javascript filter array multiple values
javascript filter array of objects by array of strings
javascript filter object
javascript array.filter multiple arguments
filter array of arrays javascript

I am trying to filter data inside array object of array object, Please find below code for more information.

var data = [
  {
    name:'testdata1',
    subdata:[{status:'fail'},{status:'success'}] 
  },
  {
    name:'testdata2',
    subdata:[{status:'fail'},{status:'success'}] 
  }
]

Expected Data:

var successdata = [
  {
    name:'testdata1',
    subdata:[status:'success'}]
  },
  {
    name:'testdata2',
    subdata:[status:'success'}] 
  }
];

var FailureData =[
  {
    name:'testdata1',
    subdata:[{status:'fail'}]
  },
  {
    name:'testdata2',
    subdata:[{status:'fail'}] 
  }
];

I missed curly braces,So i am updating


Hope this helps.

const data = [{
    name: 'testdata1', subdata: [{status: 'fail'}, {
        status:
            'success'
    }]
},
    {
        name: 'testdata2', subdata:
            [{status: 'success'}, {status: 'fail'}]
    }
];

const filterData = (data, status) => data.reduce((acc, val) => {

    const sub = val.subdata.map((v) => v.status === status ? ({ name: val.name, subdata: [v] }) : null).filter(f => f !== null);

    return acc.concat(sub);
}, []);

const successData = filterData(data, 'success');
const failureData = filterData(data, 'fail');

console.log('successData', successData);
console.log('failureData', failureData);

(Note that because `price` and such are given as strings in your object, // the var results = jLinq.from(data.users) . List which has built in filtering methods and extends the standard javascript array (and also grouping, sorting and finding​). JavaScript Array filter() method in detail. The following illustrates the syntax of the filter() method: arrayObject.filter (callback, contextObject); The filter() method creates a new array with all the elements that pass the test implemented by the callback() function.


You could map your arrays using Array.map():

var successData = data.map(item => ({name: item.name, subdata:[{status:'success'}]})

Something like: var data = [ {id: 1, string: "Lets go home"}, {id: "John is The Object.values method gives an array, so we can filter that, but in  filter() Array Method in JavaScript filter() creates a new array with elements that fall under a given criteria from an existing array: var numbers = [1, 3, 6, 8, 11]; var lucky = numbers.filter(function(number) { return number > 7; }); // [ 8, 11 ]


What I guess you want to do is filter the array based on subdata status. I also guess that what subdata should have is just the status property and your code would be: var data = [{name:'testdata1',subdata:[{status:'fail'},{status:'success'}] }.

Then you want to look in the subdata array and find which data have success and failure in them.

So what you could be looking for is this:

var successData = data.filter(sdata => { 
   var successFlag=false;
   sdata.subdata.forEach(subdata=>{
       if (subdata.status==='success'){
            successFlag = true;
       }
   }
   return successFlag;
}  

The same with the failureData.

For more information you could check the Array.prototype.filter function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

P.S. As mentioned in a comment to your question as well, your subdata array cannot be an object with two of the same property

JavaScript Array filter() Method Return an array of all the values in the ages array that are 18 or over: The array object the current element belongs to  Filter every object to check if surnames exists. If yes, copy object using Object.assign and set subElements value to filtered list. Create a temp array to hold all similar objects and push copied object to it. Push this array to final array on every iteration of distinct surname.


var data = [{name:'testdata1',subdata:[{status:'fail'}, {status:'success'}] },{name:'testdata2',subdata:[{status:'success'}, {status:'fail'}] }]


var successData = filterByStatus('success', data);
var failureData = filterByStatus('fail', data);

function filterByStatus(status, data) {
  return data.map(d => {
   var newObj = Object.assign({}, d);
   newObj.subdata = newObj.subdata.filter(s => s.status === status);
   return newObj;
  });
}

console.log('successData', successData);
console.log('failureData', failureData);

You can search in the whole array for a value like this: var young = people.filter((​obj) => { var flag = false; Object.values(obj).forEach((val) => { if(String(val). function filter(arr, ids, gender) { // takes an array of people arr, an array of indexes ids, and a gender and return the matched people objects from arr return arr.filter(function(obj) { // filtering each object


The array .filter() method iterates through a given array and returns a new array which matches a rule JavaScript Glossary - Lesson 7 of 29 thisArg This is an argument passed to be used as the this value in the callback - optional The .​filter() method can be used to filter an array of objects which share a similar property. The filter () method creates an array filled with all array elements that pass a test (provided as a function). Note: filter () does not execute the function for array elements without values. Note: filter () does not change the original array. Browser Support. The numbers in the table specify the first browser version that fully supports the


arr.filter() function is used to create a new array from a given array consisting of only In this example the function filter() creates a new array consisting of only those How to compare two JavaScript array objects using jQuery/JavaScript ? LEARN; Algorithms · Data Structures · Languages · CS Subjects · Video Tutorials. As you can see, using .reduce() is an easy way to generate a single value or object from an array. .filter() What if you have an array, but only want some of the elements in it?


In my last blog post, I wrote about the array map method in JavaScript. In case you Example 1: Filtering an array of objects. If you have an  For example, I want to delete "A" and "D" out of my Object Array because the the first value in "data" array in A contains [0,1],[0,1],[0,2],[0,4] and the first data in "D" contains "m" which is not the number. I would be very grateful if you could help me with this problem. Thank you! Answer: You can use the aptly-named Array.prototype.filter.