0
tradeArray = ['ITD', 'TND', 'TRD']
trade = []

this.tradeArray.map(val => this.trade.push({
      "value": val,
      "checked": false,
}))

I have this snippet and I want to add one more field to trade array, counter.

For example, if I have count = [3, 5, 2] I want to obtain:

0: {value: "ITD", checked: false, counter: 3}
1: {value: "TND", checked: false, counter: 5}
2: {value: "TRD", checked: false, counter: 2}

I hope by this example you will understand what I want to obtain. I tried something like this:

count = [3, 5, 2]
this.tradeArray.map(val => this.trade.push({
  "value": val,
  "checked": false,
  "counter": this.count.map(val => val)
}))

but this will give me

0: {value: "ITD", checked: false, counter: [3, 5, 2]}
1: {value: "TND", checked: false, counter: [3, 5, 2]}
2: {value: "TRD", checked: false, counter: [3, 5, 2]}

How can I modify in order to obtain what I want? Thank you for your time!

5
  • 1
    From where did you get this information countITD = 3, countTND = 5, countTRD = 2 Commented Nov 14, 2018 at 15:25
  • They are just counters, how many times ITD, TRD and TND appear in a list. Commented Nov 14, 2018 at 15:26
  • 1
    i guess you don't understand what the map function is for. The map function takes every value of a list and mutates it with the function. the return value will be a list with the mutated values. What you are doing is not mapping the values but looping through them, use forEach Commented Nov 14, 2018 at 15:29
  • 1
    @Isitar you may want to avoid using the word "mutate" when the function does not actually "mutate" the original value Commented Nov 14, 2018 at 15:34
  • @FedericoklezCulloca You're right, it produces a new transformed value. Commented Nov 14, 2018 at 15:43

3 Answers 3

3

First of all, don't use map just to iterate over collections. Instead, use it for what it is meant to be used: It projects a function to every item in a collection and creates a new collection (Array in your case) from the return values of the function. If you just want to iterate, use forEach instead.

There are multiple possible solutions, but basically what you want is some association between the "trade" in tradeArray and the values inside your counts. You can use index positions for that:

var countITD = 3, countTND = 5, countTRD = 2;

var tradeArray = ['ITD', 'TND', 'TRD']
var counts = [countITD, countTND, countTRD]

var trade = tradeArray.map((name, index) => {
  return {value: name, checked: false, counter: counts[index]};
});
// -> [{value: 'ITD', checked: false, counter: 3}, ..., {value: 'TRD', checked: false, counter: 2}]
Sign up to request clarification or add additional context in comments.

Comments

2

You want to match the index of tradeArray to same index in count array to get the associated value from count

const tradeArray = ['ITD', 'TND', 'TRD'],
  count = [3, 5, 2];
  
const res = tradeArray.map((v,i)=> ({value:v, checked:false, count:count[i]}));

console.log(res)

Comments

0

If you're sure the count array is in the same order as the tradeArray, you can use the index .map provides

count = [3, 5, 2]
this.trade = this.tradeArray.map((val, index) => {
  "value": val,
  "checked": false,
  "counter": count[index]
})

1 Comment

This answer misuses the map method. Use forEach instead

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.