简体   繁体   English

如何在Javascript中合并数组?

[英]How to merge array in Javascript?

I have built my array as following. 我建立了我的数组如下。

var myArray = [];
for(var i = 0; i < data.length; i++)
{
    var product = data[i][0];
    var size = data[i][1];
    myArray.push({product:product, value:size})
}

How the data has been constructed is out of the scope. data构造方式超出了范围。 When I loop through myArray I get the following result in the console.log. 当我遍历myArray ,在console.log中得到以下结果。

for(var i = 0; i < myArray.length; i++)
{
    var product = myArray[i].product;
    var size = myArray[i].value;
    console.log(product + ' => ' + size);
}

Shoe A => S
Shoe A => M
Shoe B => XS
Shoe B => S
Shoe B => M
Shoe C => M
Shoe C => LG
Shoe D => S
Shoe D => M
Shoe D => LG
Shoe D => XL

How should I do to get the following result? 如何获得以下结果?

Shoe A => [S, M]
Shoe B => [XS, S, M]
Shoe C => [M, LG]
Shoe D => [S, M, LG, XL]

I tend to post myArray to a PHP handler using AJAX. 我倾向于使用AJAX将myArray发布到PHP处理程序。

To get desired output in single loop: 要在单循环中获得所需的输出:

Shoe A => [S, M] 鞋子A => [S,M]

Shoe B => [XS, S, M] 鞋子B => [XS,S,M]

Shoe C => [M, LG] 鞋C => [M,LG]

Shoe D => [S, M, LG, XL] 鞋D => [S,M,LG,XL]

You can try this solution. 您可以尝试此解决方案。 Use hash instead of array. 使用哈希而不是数组。

var myArray = {};

for(var i = 0; i < data.length; i++)
{
    var product = data[i][0];
    var size = data[i][1];
    if(myArray[product] === undefined) {
       myArray[product] = []
    }
    myArray[product].push(size)
}

You should be able to use Array.reduce() to add each value to a new Object : 您应该能够使用Array.reduce()将每个值添加到新的Object

[
  {product:'shoe a', value:'L'},
  {product:'shoe b', value:'M'},
  {product:'shoe b', value:'S'}

].reduce(function(memo, item) {
  var arr = memo[item.product] || [];
  arr.push(item.value);
  memo[item.product] = arr;
  return memo
}, {})

I think you want to build a structure like this: 我认为您想构建这样的结构:

var oData = {
  A : [Some Sizes],
  B : [Some Sizes] ...
}

where A and B are your products. 其中A和B是您的产品。 So when you loop over your initial data you need to: 因此,当您遍历初始数据时,您需要:

var sSize = "size you want to add";
    if(oData[A] === undefined){
       oData.A = [sSize];
    }else{
       oData[A].push(sSize);
    }

your output array list should have object like this { name:"productName",size: [{ "productSize"}] 您的输出数组列表应具有这样的对象{name:“ productName”,size:[{“ productSize”}]

look example, aaa is output array 看例子,aaa是输出数组

    var aaa = [];
        var names = [];

      for(var i = 0; i < myArray.length; i++)
        {
                if(names.indexOf(myArray[i].product)>-1){
                        for(var j=0; j< aaa.length; j++){
                            if(aaa[j].name == myArray[i].product){
                               aaa[j].size.push(myArray[i].value)
                             }
                     }
            }
            else{
                var ppp = {};
                ppp.name = "";
                ppp.size = [];
                ppp.name =  myArray[i].product;
                ppp.size.push(myArray[i].value);
                names.push(ppp.name);
                aaa.push(ppp);
            }
        }

        for(var i=0; i<aaa.length; i++ ){
        console.log(aaa[i]);
        }

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

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