【问题标题】:Can I add specific data to an attribute of an object?我可以将特定数据添加到对象的属性吗?
【发布时间】:2019-10-29 16:37:36
【问题描述】:

我知道下面的代码不正确,但我认为它会表明意图。这可能吗?我希望这个函数更新原始数组,所有 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"},
];

【问题讨论】:

标签: javascript arrays function object


【解决方案1】:

一种方法是使用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);

【讨论】:

    【解决方案2】:

    你想就地修改你的数组,所以你需要

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

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

    有不同的可能性,最简单的技术是循环使用for,就像你已经尝试过的一样

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

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

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

    【讨论】:

    • 非常感谢您绞尽脑汁思考过程。这真的帮助了我的理解:) 谢谢!!!
    【解决方案3】:

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

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

    说明:我们遍历汽车数组的元素,并将 orderno 数字逻辑分组为 5 组。

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

    RegExp 起初可能看起来有点混乱,但在处理字符串和明显的模式时可能是一个很好的工具。

    【讨论】:

    • 这很酷。谢啦!!!我非常感谢您的帮助。
    • 干杯,很高兴能帮上忙。确保您接受最合适的答案,以供未来的搜索者发现!
    【解决方案4】:

    首先你可以使用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}))

    【讨论】:

    • 您好,谢谢,这很有用。你知道我如何通过上面的函数实现它,所以它会自动更新订单号吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2019-09-20
    相关资源
    最近更新 更多