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

标签 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

哪里:

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

这是我实际用来映射它的函数(其中 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/

相关文章:

javascript - 将 props/state 传递给父组件/从父组件传递

javascript - Angular 2+ : change detection, 属性更改和 View 更新之间的状态

javascript - 调用命名函数表达式

php - 发送 POST 数组和内存泄漏

c++ - 素数计算器C++

java - 如何退出Java循环?基本猜谜游戏中的 while 循环

php - 在php中的foreach循环中获取特定记录

javascript - 当源自 API 并映射到变量时,React 图像不会呈现

javascript - 如何通过数组更新 Angular 子范围

c# - 按后续负数的数量对数组的(先前)元素进行分组