-1

I have Javascript array that looks like this:

 fruitsGroups: [
    "apple0",
    "banana0",
    "pear0",
  ]

How can I increase the number of each item in this array?

 fruitsGroups: [

    "apple0",
    "apple1",
    "apple2",

    "banana0",
    "banana1",
    "banana2",

    "pear0",
    "pear1",
    "pear2"
  ]
Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
John
  • 627
  • 6
  • 19
  • 1
    Nothing about that is specifically more jQuery than it is just javascript. – Taplar Mar 19 '18 at 16:57
  • 4
    Using a loop. What have you tried? – j08691 Mar 19 '18 at 16:57
  • @j08691 I am not sure how I can use loop to push elements in array – John Mar 19 '18 at 16:59
  • @Taplar this is a small fragment from the whole jQuery code – John Mar 19 '18 at 16:59
  • This fragment is showing a part of an object that has a key with a value of an array. Both objects and arrays, plain javascript, not jQuery. My point being you are asking about a javascript logic issue, and framing it as a jQuery question, which it is not. – Taplar Mar 19 '18 at 17:00
  • It seems that you forgot to include your own efforts, Stack Overflow is not a write-my-code-for-me service, so please [edit your question](https://stackoverflow.com/posts/49368412/edit) and add all relevant code that you already wrote into it. Also please see **[ask]**. And if needed, see this: https://stackoverflow.com/q/586182/1220550 – Peter B Mar 19 '18 at 17:03

5 Answers5

2

Since we have 2018 already, another approach using Array.map and destructuring:

const groups = [
    "apple0",
    "banana0",
    "pear0",
  ];

[].concat(...groups.map(item => [
    item,
    item.replace(0, 1),
    item.replace(0, 2)
  ]
))

// result: ["apple0", "apple1", "apple2",
//          "banana0", "banana1", "banana2",
//          "pear0", "pear1", "pear2"]

Explanation:

groups.map(item => [item, item.replace(0, 1), item.replace(0, 2)]) takes each array item one by one (apple0, then banana0, …) and replaces it with an array of:

  • item – the item itself (apple0)
  • item.replace(0, 1) – the item with zero replaced by 1 (apple1)
  • item.replace(0, 2) – the item with zero replaced by 2 (apple2)

so the array looks like…

[
  ["apple0", "apple1", "apple2"],
  ["banana0", "banana1", "banana2"],
  ["pear0", "pear1", "pear2"],
]

…and then we need to flatten it, that's the [].concat(... part. It basically takes array items (the three dots, read more about destructuring here), and merges them into an empty array.

If you want to replace any digit, not just zero, use regular expression:

"apple0".replace(/\d$/, 1)
 // -> "apple1"
"apple9".replace(/\d$/, 1)
 // -> "apple1"
  • \d – any number character
  • $ - end of line
  • the surrounding slashes tell JS that it's a regular expression, you could use new RegExp("\d$") instead, too
helb
  • 3,154
  • 15
  • 21
1

I think your looking for something like that?

var fruitsGroups = [
    "apple0",
    "banana0",
    "pear0",
  ];
  console.log(fruitsGroups);

  var newFruits = [];
  $.each(fruitsGroups, function(i, j) {
     var n = parseInt(j.substring(j.length - 1));
     for(var k = 0; k < 3; k++) {
        newFruits.push(j.substring(0, j.length - 1) + (n + k));
     }
  });

  console.log(newFruits);
Dionei Miodutzki
  • 657
  • 7
  • 16
1

You could create function that uses reduce() method and returns new array.

function mult(data, n) {
  return data.reduce((r, e) => {
    return r.push(e, ...Array.from(Array(n), (_, i) => {
      const [text, n] = e.split(/(\d+)/);
      return text + (+n + i + 1)
    })), r
  }, []);
}

console.log(mult(["apple0", "banana0", "pear0"], 2))
console.log(mult(["apple4", "banana2", "pear0"], 3))
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

I have tried this for you, it might help as per my understanding. Naive Approach

var a = ['apple0','banana0','pearl0']
var fruitGroups = []
for(var i=0; i < a.length; i++){
 for(var j = 0; j<a.length; j++){
   let fruit = a[i].replace(/\d/g,'')+j
   fruitGroups.push(fruit)
  }
}
console.log(fruitGroups)
Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
0

Map the original array. For each item, create a sub array, and fill it with the current item. Then map the sub array and replace the digit/s of each item with the index. Flatten the sub arrays, by spreading into Array.concat():

const fruitsGroups = [
  "apple0",
  "banana0",
  "pear0",
];

const len = 3;
// map the original array, and use concat to flatten the sub arrays
const result = [].concat(...fruitsGroups.map((item) => 
  new Array(len) // create a new sub array with the requested size
  .fill(item) // fill it with the item
  .map((s, i) => s.replace(/\d+/, i)) // map the strings in the sub array, and replace the number with the index
));

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209