0

I would like to have an output of an array number_list = [1, 4]. What I have overwrites my array. Any Ideas? Thanks

data = [{"key":"50", "name": "James", "$": "1"},
        {"key":"20", "name": "George", "$": "4"}]

$.each(data, function(){  
    number = this.$  
    var number_list = number.push(this.$)  
})
OwenCowley
  • 55
  • 5
  • 1
    To fix your code, use `var number_list = [];` before the $.each, then do `number_list.push(this.$);` inside. (your var is local to the function and will be recreated each time and not exist outside. Also, you're doing `this.$.push(this.$)`, which doesn't make sense) –  Oct 07 '20 at 11:42
  • "*strip out*" - generally means remove - so you'd be left with `{key:50,name:james}` - is that what you wanted? or did you mean *extract*? – freedomn-m Oct 07 '20 at 11:53

1 Answers1

1

try:

const data = [{"key":"50", "name": "James", "$": "1"},
              {"key":"20", "name": "George", "$": "4"}]

const number_list = data.map(i => i.$);
Iosif
  • 812
  • 2
  • 15