0

How to pass a variable to an array key where the array is an another variable that is inside a curly bracket. In the following code I'm trying to push values from source array if the value is greater than 50.

render(){
  var source_arr = [35,45,50,60,20];
  var dest_array = [];
  for(var x=0; x<source_arr.length; x++){
    if(source_array[x]>50){
      dest_array.push({source_array[x]});
    }
  }
  return(
    <div>
    </div>
  )
}

Here I'm not able to pass the value of variable 'x' as the array key inside the for loop.

Lalas M
  • 1,116
  • 1
  • 12
  • 29
  • 5
    It's not at all clear what you want to do here. Please show us examples of inputs and the results you expect. For one thing, the above doesn't make a lot of sense, since `source_arr` is empty and you'll never go into the loop. – T.J. Crowder Sep 26 '19 at 07:48
  • I don't think you should use push as that mutates dest_array, Maybe do `new_dest=dest_array.concat(source_array.filter(item=>condition)` – HMR Sep 26 '19 at 07:49
  • @HMR - It's a local, mutating it is perfectly fine. No reason to constantly create new arrays here. But depending on what the OP wants, `filter` might be the way to go... – T.J. Crowder Sep 26 '19 at 07:52
  • @T.J.Crowder i have edit the question. – Lalas M Sep 26 '19 at 08:16
  • @LalasM - That doesn't address my comment above. What result do you want? – T.J. Crowder Sep 26 '19 at 08:17

2 Answers2

0

You need to make use of [] to define keys

render(){
  var source_arr = [];
  var dest_array = [];
  for(var x=0; x<source_arr.length; x++){
    if(a condition){
      dest_array.push({[source_array[x]]:source_array[x] });
    }
  }
  return(
    <div>
    </div>
  )
}
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • @LalasM "some syntax error" is useless. ***What*** syntax error? The above is valid ES2015+. If this is what you're trying to do, and you need to do it in ES5, see [this question's answers](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name). – T.J. Crowder Sep 26 '19 at 08:16
  • @T.J.Crowder syntax error is rectified. thanks Shubham Khatri syntax error was a typo – Lalas M Sep 26 '19 at 08:17
  • @LalasM - I don't know what you mean. The answer above has not been edited since your comment was made. – T.J. Crowder Sep 26 '19 at 08:18
0

Looks like you want to map all items in source array to objects if a condition is true or false:

const source_arr = [
  {name:'hi',age:22},
  {name:'bye',age:88},
];
const dest_arr = source_arr
  .map(
    //map item to object or false
    item => item.age<40 && { [item.name]: item.age }
  )
  .filter(x => x); //remove all false items
console.log(dest_arr);

You can combine the map and filter in one reduce:

const source_arr = [
  { name: 'hi', age: 22 },
  { name: 'bye', age: 88 },
];
const dest_arr = source_arr.reduce(
  (result, item) =>
    //add object if condition is true
    item.age < 40
      ? result.concat({ [item.name]: item.age })
      : result,
  []
);
console.log(dest_arr);
HMR
  • 37,593
  • 24
  • 91
  • 160