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: [[], []]
4
  • 1
    Can you be more elaborate on what you want, and why cannot you directly use a loop? Commented Feb 20, 2020 at 9:20
  • Does this answer your question? For-each over an array in JavaScript? Commented Feb 20, 2020 at 9:20
  • You are talking about a stream object yet your array look pretty standard to me. Could you include a minimal reproducible example with your issue? Commented Feb 20, 2020 at 9:29
  • your arr is an array of arrays, each can be iterated Commented Feb 20, 2020 at 9:36

2 Answers 2

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 !== ""))
Sign up to request clarification or add additional context in comments.

Comments

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);

2 Comments

@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
Ya you are right, It also worked for me than you and it is much more readable and understandable. Thanx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.