简体   繁体   English

增加Javascript数组中项目的值

[英]Increase value of item in Javascript array

I have Javascript array that looks like this: 我有看起来像这样的Javascript数组:

 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"
  ]

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);

You could create function that uses reduce() method and returns new array. 您可以创建使用reduce()方法并返回新数组的函数。

 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)) 

Since we have 2018 already, another approach using Array.map and destructuring: 由于我们已经有了2018年,因此可以使用Array.map和解构的另一种方法:

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: groups.map(item => [item, item.replace(0, 1), item.replace(0, 2)])一对一地获取每个数组项( apple0 ,然后banana0 ,…),并将其替换为数组的:

  • item – the item itself ( apple0 ) item –项目本身( apple0
  • item.replace(0, 1) – the item with zero replaced by 1 ( apple1 ) item.replace(0, 1) –零被1代替的项目( apple1
  • item.replace(0, 2) – the item with zero replaced by 2 ( apple2 ) item.replace(0, 2) –零被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. …然后我们需要将其展平,这是[].concat(...部分。它基本上需要数组项(三个点, 在此处了解有关解构的更多信息 ),并将它们合并为一个空数组。

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 \\d –任何数字字符
  • $ - end of line $ -行尾
  • the surrounding slashes tell JS that it's a regular expression, you could use new RegExp("\\d$") instead, too 周围的斜杠告诉JS这是一个正则表达式,您也可以使用new RegExp("\\d$")

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) 

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() : 通过扩展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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM