0

I am using javascript and trying to duplicate an array item based off of the number of matches that are inside each item. I believe I need to use arr.map() to accomplish this but am having trouble with the evaluation logic.

So in the example below Item 1 would be duplicated 3 times and Item 2 would be duplicated 1 time. Thanks your help and your time.

My current array looks like:

"cards" = [
    {
        "text": "Item 1"
        "matches": 3
    }
    {
        "text": "Item 2"
        "matches": 1
    }
]

New Array would look like:

"newCards" = [
    {
        "text": "Item 1"
        "matches": 3
    }
    {
        "text": "Item 1"
        "matches": 3
    },
    {
        "text": "Item 1"
        "matches": 3
    },
    {
        "text": "Item 2"
        "matches": 1
    },
    {
        "text": "Item 2"
        "matches": 1
    }
]

attempting to use map function

let cardCounter = 0

let cardMap = cards.map(function (item){
cardCounter++
if (cardCounter < item.matches) {
 // Not really sure what to return here....
 // tried to return
 // return[item, item]
 // this returns an array where the first two keys are arrays and the rest undefined.
}

})
5
  • 1
    I don't think map is the right tool for the job, unless you intend to return arrays and then flatten the result. Commented Oct 15, 2018 at 19:59
  • Either .forEach() or .reduce() would be more appropriate than .map() Commented Oct 15, 2018 at 20:04
  • Should the duplicates be copies of the object or multiple references to the same object? Commented Oct 15, 2018 at 20:05
  • Thanks for you suggestions @barmar they would be exact duplicates of each object Commented Oct 15, 2018 at 20:08
  • That doesn't really answer my question. References to the same object are exact duplicates. Do you need to be able to modify one of them without affecting the other? Commented Oct 15, 2018 at 20:11

2 Answers 2

1

I don't think map is a good choice as it does one value to one value mapping. If you have lodash available you could use flatMap in which case your return[item, item] idea would work. If not then perhaps use reduce. Something like:

let cardMap = cards.reduce(function (acc, item){
      for (let i = 0; i < item.matches; i++) {
             acc.push(item);
      }
      return acc;
}, []);
Sign up to request clarification or add additional context in comments.

3 Comments

What is item.test, is that a typo for item.text? The example in the question shows that the result should contain objects, not just one property.
Thank you this worked. As I ended up pushing the entire object back into the array instead of just the text but all in all it worked. acc.push(item) thanks for your expertise
Thanks @Barmar, I've cleaned it up
0

How about this:

var cards = [
    {
        "text": "Item 1",
        "matches": 3
    },
    {
        "text": "Item 2",
        "matches": 1
    }
]

var newCardsArray = [];

cards.forEach(function(element) {
    for(i = 0; i < element.matches; i++) {
        newCardsArray.push(element);
    }
});

console.log(newCardsArray);

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.