0

I have this fixed array:

x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"}
   {value: 3, text: "anything else?"}
   {value: 4, text: "other dummy text"}
]

and another dynamic array:

y = [2, 4]

I would like to filter the array "x" based on the values from array "y"

the expected result should be:

x = [
   {value: 2, text: "no problem"},
   {value: 4, text: "other dummy text"}
]

How could I do this?

Thanks

Daniel
  • 73
  • 1
  • 6

4 Answers4

3

After you get the dynamic array value. You can run the code like this it filters the array for the dynamic array value

let x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"},
   {value: 3, text: "anything else?"},
   {value: 4, text: "other dummy text"}
];

const y=[2,4];


x = x.filter( data => (y.includes(data.value)));

console.log(x);
innocent
  • 864
  • 6
  • 17
  • [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). – Andy May 04 '22 at 08:28
3

what you are looking for is to filter array x, and check if its values is included in array y

const x = [
  { value: 0, text: "hello world" },
  { value: 1, text: "how are you?" },
  { value: 2, text: "no problem" },
  { value: 3, text: "anything else?" },
  { value: 4, text: "other dummy text" }
];
const y = [2, 4];

const result = x.filter((item) => y.includes(item.value));

console.log(result);
Lukáš Gibo Vaic
  • 3,960
  • 1
  • 18
  • 30
  • [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). – Andy May 04 '22 at 08:28
  • Isn't that the same answer, that @innocent gave a minute earlier? – David May 04 '22 at 08:30
  • @David that might happen, he might have posted it while I was writing my solution – Lukáš Gibo Vaic May 04 '22 at 08:36
0

There you go.

x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"},
   {value: 3, text: "anything else?"},
   {value: 4, text: "other dummy text"}
]
y = [2, 4]
z = []
for (var i = 0;i<x.length;i++){
    for (var j = 0;j<y.length;j++){
        if (x[i].value == y[j]){z.push(x[i])}
    }
}
console.log(z)
mrtechtroid
  • 658
  • 3
  • 14
  • I belive in this day and age, we can do this without two nested for loops and unnecessary mutations, ever since we got map/filter/reduce I would highly recommended using those rather than for loops – Lukáš Gibo Vaic May 04 '22 at 08:26
  • @LukášGiboVaic why would the for loop be bad than map filter and reduce was just wondering. From what I see that would only result in short code but the tc would be same. Also from what I was reading this for loop will run faster than forEach loop I guess – innocent May 04 '22 at 08:33
  • @David I view this very differently, if you use something way less readable and something community is going away from, and you are teaching that to newcommers, its not very ideal thing to do. – Lukáš Gibo Vaic May 04 '22 at 08:36
0

Another way uses .reduce:

const data = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"},
   {value: 3, text: "anything else?"},
   {value: 4, text: "other dummy text"}
];

function selectValuesFrom(xs, ys) {
  return ys.reduce( // fold ys into a new array
    (a, y) => {
      // try to find an "x" by the given "y" value
      const r = xs.find(x => x.value === y);
      // if there is an "x", add it to the new array, otherwise
      // skip it
      return r ? a.concat(r) : a;
    },
    []
  );
}

console.log(selectValuesFrom(data, [2, 4]));
console.log(selectValuesFrom(data, [3, 1, 5]));
David
  • 3,552
  • 1
  • 13
  • 24