是否可以将特定数据添加到对象的属性?

时间:2019-10-29 16:37:36

标签: javascript arrays function object

我知道下面的代码是不正确的,但是我认为它可以表明意图。这可能吗?我希望此函数更新原始数组,所有orderno的格式都为000.0.000.00000.0。

let cars= [ 
  {orderno: "5766302385925", make: "Alfa", dealership: "NA"},
  {orderno: "4663873261390", make: "Merc", dealership: "NA"},
  {orderno: "8458821291579" , make: "BMW", dealership: "EU"},
  {orderno: "3376768687480", make: "Ford", dealership: "NA"},
  {orderno: "5186132840921", make: "Buick", dealership: "EU"},
];

function addOrderDot() {
    for (var i=0; i<cars.length; i++) {
    cars.orderno[i].splice(2,0,'.')
    cars.orderno[i].splice(3,0,'.')
    cars.orderno[i].splice(6,0,'.')
    cars.orderno[i].splice(11,0,'.')
    } 
}

函数后所需的数组:

let cars= [ 
  {orderno: "576.6.302.38592.5", make: "Alfa", dealership: "NA"},
  {orderno: "466.3.873.26139.0", make: "Merc", dealership: "NA"},
  {orderno: "845.8.821.29157.9" , make: "BMW", dealership: "EU"},
  {orderno: "337.6.768.68748.0", make: "Ford", dealership: "NA"},
  {orderno: "518.6.132.84092.1", make: "Buick", dealership: "EU"},
];

4 个答案:

答案 0 :(得分:0)

一种方法是使用map()slice()

let cars= [ 
  {orderno: "5766302385925", make: "Alfa", dealership: "NA"},
  {orderno: "4663873261390", make: "Merc", dealership: "NA"},
  {orderno: "8458821291579", make: "BMW", dealership: "EU"},
  {orderno: "3376768687480", make: "Ford", dealership: "NA"},
  {orderno: "5186132840921", make: "Buick", dealership: "EU"},
];

function addOrderDot(cars) {
  return cars.map(car => ({
    ...car,
    orderno: car.orderno.slice(0, 3) + '.' +
      car.orderno.slice(3, 4) + '.' +
      car.orderno.slice(4, 7) + '.' +
      car.orderno.slice(7, 12) + '.' +
      car.orderno.slice(12, 13)
  }));
}

let result = addOrderDot(cars);

console.log(result);

答案 1 :(得分:0)

您要修改数组,因此需要

  1. 迭代数组的所有元素
  2. 获取元素的orderno属性的值
  3. 修改值
  4. 更新回数组元素的orderno属性

(1)遍历数组的所有元素

存在不同的可能性,最简单的技术是使用您已经尝试过的方法与for循环

 for (let i=0; i<cars.length; i++) {
 } 

(2)使用cars[i]寻址数组的元素,并使用errorno访问cars[i].orderno属性

 let orn = cars[i].orderno;

(3)修改其值

orn = orn.slice(0,3)+'.'+orn.slice(3,4);

(4)在数组上直接分配新值

cars[i].orderno = orn;

因此,将所有部分放在一起

 for (var i=0; i<cars.length; i++) {
    let orn = cars[i].orderno;
    orn = orn.slice(0,2)+'.'+orn.slice(2,4) ;
    cars[i].orderno = orn;
 }

答案 2 :(得分:0)

更优雅的解决方案是使用简单的正则表达式替换每个字符串:

let cars= [ 
  {orderno: "5766302385925", make: "Alfa", dealership: "NA"},
  {orderno: "4663873261390", make: "Merc", dealership: "NA"},
  {orderno: "8458821291579" , make: "BMW", dealership: "EU"},
  {orderno: "3376768687480", make: "Ford", dealership: "NA"},
  {orderno: "5186132840921", make: "Buick", dealership: "EU"},
];

function addOrderDot() {
    cars.forEach(car => {
       car.orderno = car.orderno.replace(
      /^(.{3})(.{1})(.{3})(.{5})(.{1})$/,
      `$1.$2.$3.$4.$5`
    )
    })
}

说明:我们遍历cars数组的元素并将逻辑上的orderno数字分成5组。

在regExp中,^表示字符串的开头,()定义了一个逻辑组,以后可以将其用作变量,。{number}个字符,以及$字符串的末尾。 因此,我们的正则表达式将一组3个字符分成$ 1,将一组1个字符分成$ 2,依此类推。 然后,我们仅将初始字符串替换为“缝合”逻辑组。

RegExp乍一看似乎有些令人困惑,但在处理字符串和明显的模式时可能是一个很好的工具。

答案 3 :(得分:-1)

首先,您可以使用map转换数组:

let cars= [ 
  {orderno: "5766302385925", make: "Alfa", dealership: "NA"},
  {orderno: "4663873261390", make: "Merc", dealership: "NA"},
  {orderno: "8458821291579" , make: "BMW", dealership: "EU"},
  {orderno: "3376768687480", make: "Ford", dealership: "NA"},
  {orderno: "5186132840921", make: "Buick", dealership: "EU"},
];

console.log(cars.map(car => {car.orderno = "test"; return car}))