Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filter array inside array

//you can do a .map to iterate over the first level array
// and return an object that have all the other things in the array with spred operator
// and add a new property that will be the second level array filtered
arrayTier1.map((item) => {
        return ({...item, arrayTier2: item.arrayTier2.filter(subitem => subitem.value == 2)});
Comment

filter array with an array

/* Here's an example that uses (some) ES6 Javascript semantics to filter an object array by another object array. */

        // x = full dataset
        // y = filter dataset
        let x = [
            {"val": 1, "text": "a"},
            {"val": 2, "text": "b"},
            {"val": 3, "text": "c"},
            {"val": 4, "text": "d"},
            {"val": 5, "text": "e"}
            ],
            y = [
            {"val": 1, "text": "a"},
            {"val": 4, "text": "d"}               
            ];

        // Use map to get a simple array of "val" values. Ex: [1,4]
        let yFilter = y.map(itemY => { return itemY.val; });

        // Use filter and "not" includes to filter the full dataset by the filter dataset's val.
        let filteredX = x.filter(itemX => !yFilter.includes(itemX.val));

        // Print the result.
        console.log(filteredX);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: check template shopify 
Javascript :: javascript selection in iframe 
Javascript :: queryselector name attribute 
Javascript :: copy array javascript 
Javascript :: javascript dynamic import 
Javascript :: js php number format 
Javascript :: ajax code 
Javascript :: nodejs get param cli 
Javascript :: inline style react 
Javascript :: js get element by attribute 
Javascript :: get table row data jquery 
Javascript :: sort object array javascript 
Javascript :: react how to update state array 
Javascript :: vue cdn 
Javascript :: how to slice/trim/remove last character in string 
Javascript :: ajax with progress bar 
Javascript :: react axios get cookie from response 
Javascript :: ionic capacitor keyboard push content up 
Javascript :: canvas js filter greyscale 
Javascript :: js for loop array 
Javascript :: index.js:3 Uncaught ReferenceError: $ is not defined at index.js:3 
Javascript :: hex string to int javascript 
Javascript :: fetch await reactjs 
Javascript :: set span text jquery 
Javascript :: how to remove minutes with moment js 
Javascript :: javascript cancel timeout 
Javascript :: javascript format date 
Javascript :: js exit function 
Javascript :: check if item not in array node js 
Javascript :: font google expo 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =