103#customerName>Atelier graphique#contactL-6ren">
gpt4 book ai didi

javascript - JS - 将字符串映射到对象数组 ¿如何更改键名?

转载 作者:行者123 更新时间:2023-11-29 16:04:02 24 4
gpt4 key购买 nike

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

哪里:

  • “~”分行
  • “#”分列
  • ">"拆分属性:值

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

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

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

最佳答案

从:

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

到:

 { customerNumber:103, customerName:"Atelier" }

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

    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>

关于javascript - JS - 将字符串映射到对象数组 ¿如何更改键名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35928511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com