Gaurav Singla
Gaurav Singla

Reputation: 51

How to remove element from array if value is 0 in multiple dimensional array using javascript but keeping first occurance of array with value 0

UPDATE: (From comments I came to know that the questioner has additional requirement, not only removing array with value 0 ):

I want to remove elements from array having 0 value but keeping the first occurrence of array with value zero.

   var results = [

 ["Total", "Date"],
 ["2020-12-08T00:00:00.000Z", 6],
 ["2020-12-07T00:00:00.000Z", 0],
 ["2020-12-08T00:00:00.000Z", 156],
 ["2020-12-09T00:00:00.000Z", 0],
 ["2020-12-11T00:00:00.000Z", 0],
 ["2020-12-08T00:00:00.000Z", 34],
 ["2020-12-13T00:00:00.000Z", 0]];
 for (var i =0; i < results.length; i++) {
        if (results[i][1] == 0) 
            results.splice(i, 1);
        
    }
console.log(results);

i am not sure if splice will work on this situation. My array length may decrease or increase as data is coming dynamically.

In the above scenario my Output should be:

[["Total", "Date"], ["2020-12-08T00:00:00.000Z", 6], ["2020-12-07T00:00:00.000Z", 0], ["2020-12-08T00:00:00.000Z", 156], ["2020-12-08T00:00:00.000Z", 34]]

Where I only need to keep the first occurrence of array with value 0 ( ["2020-12-07T00:00:00.000Z", 0]) and keep all other subarrays without value 0.

Upvotes: 0

Views: 150

Answers (3)

lissettdm
lissettdm

Reputation: 13078

Use Array.prototype.filter()

const results = [["Total", "Date"],["2020-12-08T00:00:00.000Z", 6],["2020-12-07T00:00:00.000Z", 0],["2020-12-08T00:00:00.000Z", 156],["2020-12-09T00:00:00.000Z", 0],["2020-12-11T00:00:00.000Z", 0],["2020-12-13T00:00:00.000Z", 0]];

const output = [...results.filter(item => !item.includes(0))];
console.log(output);

Upvotes: 0

Imran Rafiq Rather
Imran Rafiq Rather

Reputation: 8118

As per your question and the comments It seems your are looking for the following dynamic solution. I have added some array elements to the results array for more clarity.

const results = [
 ["Total", "Date"],
 ["2020-12-08T00:00:00.000Z", 6],
 ["2020-12-07T00:00:00.000Z", 0],
 ["2020-12-08T00:00:00.000Z", 156],
 ["2020-12-09T00:00:00.000Z", 0],
 ["2020-12-11T00:00:00.000Z", 0],
 ["2020-12-08T00:00:00.000Z", 34],
 ["2020-12-13T00:00:00.000Z", 0]];


const firstOccurance = results.find((array,index)=>{
  return array[1]===0;
});
// console.log(firstOccurance);

const firstOccuranceIndex = results.indexOf(firstOccurance);
const array1 = results.slice(0,firstOccuranceIndex+1);
// console.log(array1);

const array2 = results.slice(firstOccuranceIndex+1);
// console.log(array2);

const updatedArray2 = array2.filter(array=>array[1]!==0);
// console.log(updatedArray2);

const finalResult =  [...array1,...updatedArray2];
console.log(finalResult);

Codepen OutPut: https://codepen.io/emmeiWhite/pen/rNMjdRZ?editors=1111

Upvotes: 2

ptothep
ptothep

Reputation: 395

You can use filter

var results = [["Total", "Date"],
["2020-12-08T00:00:00.000Z", 6],
 ["2020-12-07T00:00:00.000Z", 0],
["2020-12-08T00:00:00.000Z", 156],
["2020-12-09T00:00:00.000Z", 0],
["2020-12-11T00:00:00.000Z", 0],
["2020-12-13T00:00:00.000Z", 0]];

console.log(
results.filter((a,i)=>(a[1] !== 0) || (results.findIndex(b=>b[1]==0)==i))
);

Upvotes: 2

Related Questions