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

0

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

3 Comments

Isn't that the same answer, that @innocent gave a minute earlier?
@David that might happen, he might have posted it while I was writing my solution
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)

3 Comments

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áš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
@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.
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]));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.