简体   繁体   English

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

[英]JS - mapping string into array of objects ¿How to change key name?

 $(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> 

I get data from a source in a string like this: 我从字符串中获取数据,如下所示:

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

where: 哪里:

  • "~" splits rows “〜”分割行
  • "#" splits columns “#”拆分列
  • ">" splits propertie:value “>”分裂属性:价值

this is the function i use to map it actually (where data is the string): 这是我用来实际映射它的函数( 数据是字符串):

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

I almost got it, but i need a final help, this is what i get print at console: 我几乎得到了它,但我需要最后的帮助,这是我在控制台打印的内容:

在此输入图像描述

And what i would like to get is something like 而我想得到的是类似的东西

Array {
    customerNumber:103,
    customerName:Atelier
}

instead of: 代替:

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

I explained it the best i can, hope it's enough! 我尽力解释它,希望它足够了!

to go from: 来自:

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

to: 至:

 { customerNumber:103, customerName:"Atelier" }

if that's you want to do , you can just remap the array: 如果你想这样做,你可以重新映射数组:

  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) 


 $(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); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

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

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