0
myArray = [arr[p2][it] for (it in subarr)]

how can we have a similar syntatic sugar like python in javascript or using jquery. I don't want to create an array and push the data

sot that for doing some other calcualtions i can use .reduce((a, b) => a + b, 0);

1 Answer 1

1

You can use Array#map method for that.

let myArray = subarr.map(v => arr[p2][v]);

let arr = [{
    a: 1,
    b: 2,
    c: 3,
    d: 5
  }],
  p2 = 0,
  subarr = ['a', 'b', 'c', 'd'];

let myArray = subarr.map(v => arr[p2][v]);

console.log(myArray);

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

Comments

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.