JS - 将字符串映射到对象数组中¿如何更改密钥名称?

时间:2016-03-10 22:21:07

标签: javascript arrays loops dictionary

$(document).ready(function() {
	printData("customerNumber>103#customerName>Atelier graphique#contactLastName>Schmitt#contactFirstName>Carine #phone>40.32.2555#addressLine1>54, rueRoyale#addressLine2>#city>Nantes#state>#postalCode>44000#country>France#salesRepEmployeeNumber>1370#creditLimit>21000#~customerNumber>112#customerName>Signal Gift Stores#contactLastName>King#contactFirstName>Jean#phone>7025551838#addressLine1>8489 Strong St.#addressLine2>#city>LasVegas#state>NV#postalCode>83030#country>USA#salesRepEmployeeNumber>1166#creditLimit>71800#~customerNumber>114#customerName>Australian Collectors, Co.#contactLastName>Ferguson#contactFirstName>Peter#phone>03 9520 4555#addressLine1>636 St KildaRoad#addressLine2>Level3#city>Melbourne#state>Victoria#postalCode>3004#country>Australia#salesRepEmployeeNumber>1611#creditLimit>117300#~customerNumber>119#customerName>La Rochelle Gifts#contactLastName>Labrune#contactFirstName>Janine #phone>40.67.8555#addressLine1>67, rue des CinquanteOtages#addressLine2>#city>Nantes#state>#postalCode>44000#country>France#salesRepEmployeeNumber>1370#creditLimit>118200#~")
})

function printData(data){
	var customers = data
		.split('~')
		.map((i, e) => {
			return i
			.split('#')
			.map((i, el) => {
				return [i.split('>')[0],i.split('>')[1]];
			})

		});

	// $.each(customers, (i, el) => {
	// 	customers[i] = el.split('#');
	// 	$.each(customers[i], (name, valor) => {

	// 	})
	// })

	console.log(customers);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

我从字符串中获取数据,如下所示:

prop1>value1#prop2>value2~
prop1>value1#prop2>value2~
prop1>value1#prop2>value2

其中:

  • “〜”拆分行
  • “#”拆分列
  • “&gt;” 中splits propertie:value

这是我用来实际映射它的函数(其中数据是字符串):

function printData(data){

    var customers = data
        .split('~')
        .map((i, e) => {
            return i
            .split('#')
            .map((i, el) => {
                return [i.split('>')[0],i.split('>')[1]];
            })
        });

    console.log(customers);
}

我几乎得到了它,但我需要最后的帮助,这就是我在控制台上打印的内容:

enter image description here

我希望得到的是像

Array {
    customerNumber:103,
    customerName:Atelier
}

而不是:

Array{
    Array{
        0:customerNumber,
        1:103
    }
    Array{
        0:customerName,
        1:Atelier
    }
}

我尽力解释,希望它足够了!

1 个答案:

答案 0 :(得分:0)

来自:

[
 ["customerNumber",103     ],
 ["customerName","Atelier" ]
]

为:

 { customerNumber:103, customerName:"Atelier" }

如果你想这样做, 你可以重新映射数组:

&#13;
&#13;
    var arr = [
      [
        ["customerNumber", 103],
        ["customerName", "Atelier"]
      ],
      [
        ["customerNumber", 105],
        ["customerName", "Atr"]
      ]
    ];

    var r = arr.map(x => {
      return {
        [x[0][0]]: x[0][1],
        [x[1][0]]: x[1][1]
      }
    });

    console.log(r)
&#13;
&#13;
&#13;

&#13;
&#13;
$(document).ready(function() {
  printData("customerNumber>103#customerName>Atelier graphique#contactLastName>Schmitt#contactFirstName>Carine #phone>40.32.2555#addressLine1>54, rueRoyale#addressLine2>#city>Nantes#state>#postalCode>44000#country>France#salesRepEmployeeNumber>1370#creditLimit>21000#~customerNumber>112#customerName>Signal Gift Stores#contactLastName>King#contactFirstName>Jean#phone>7025551838#addressLine1>8489 Strong St.#addressLine2>#city>LasVegas#state>NV#postalCode>83030#country>USA#salesRepEmployeeNumber>1166#creditLimit>71800#~customerNumber>114#customerName>Australian Collectors, Co.#contactLastName>Ferguson#contactFirstName>Peter#phone>03 9520 4555#addressLine1>636 St KildaRoad#addressLine2>Level3#city>Melbourne#state>Victoria#postalCode>3004#country>Australia#salesRepEmployeeNumber>1611#creditLimit>117300#~customerNumber>119#customerName>La Rochelle Gifts#contactLastName>Labrune#contactFirstName>Janine #phone>40.67.8555#addressLine1>67, rue des CinquanteOtages#addressLine2>#city>Nantes#state>#postalCode>44000#country>France#salesRepEmployeeNumber>1370#creditLimit>118200#~")
})

function printData(data) {
  var customers = data
    .split('~')
    .map((i, e) => {
      return i
        .split('#')
        .map((i, el) => {
          var r = i.split('>');
          return {
            [r[0]]: r[1]
          };
        })

    });

  // $.each(customers, (i, el) => {
  // 	customers[i] = el.split('#');
  // 	$.each(customers[i], (name, valor) => {

  // 	})
  // })

  console.log(customers);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
&#13;
&#13;
&#13;