2

I have an array of values in a stream and you wish to pipe it such that it will emit the arrays values individually, one by one and wait for them all to be completed before processing another array

// This is the array:
let arr = [[1,2,3], [4,5,6]];

let data = arr.filter( (value) => {
  let newdata = value.filter((newVal, index) => {
    if (newVal !== value[index]) {
      return '' ;
    }
  });
});

console.log(data);
// Output: []
// Expected output: [[], []]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2
arr.map(x => x.map((y, index) => {if(y !== y[index]){return ''}}))

This will return [["", "", ""], ["", "", ""]]


For [[], []] filter out those blank strings:

arr.map(x => x.map((y, index) => {if(y !== y[index]){return ''}}).filter(z => z !== ""))
Jonathan Irwin
  • 5,009
  • 2
  • 29
  • 48
1

At root use Map instead of filter:

  let arr = [[1,2,3], [4,5,6]];

  let data = arr.map( (value) => {
    let newdata = value.filter((newVal, index) => {
      if (newVal !== value[index]) {
       return '' ;
      }
    });
    return newdata;
  });

  console.log(data);
harsh_apache
  • 433
  • 4
  • 10
  • @Rajat In above solution there is two map and one filter is using, while you can avoid an extra filter from your code. It will fix your time complexity Thanks – harsh_apache Feb 21 '20 at 10:47
  • Ya you are right, It also worked for me than you and it is much more readable and understandable. Thanx – Rajat Srivastava Feb 23 '20 at 12:09