【问题标题】:I just want to append text to an object我只想将文本附加到对象
【发布时间】:2021-04-10 04:48:30
【问题描述】:

现在对于每个具有字符串“衬衫”的项目,我想将衬衫替换为“衬衫不可用

const inventory = [
        { line_item_id: "55412", item: "Shirt small", description: "" },
        { line_item_id: "55342", item: "shirt big full", description: "" },
        { line_item_id: "1124",  item: "Pant Small",description: "",},
    ];

我希望它看起来像这样

const inventory = [
    { line_item_id: "55412", item: "Shirts are not available small", description: "" },
    { line_item_id: "55342", item: "Shirts are not available big full", description: "" },
    { line_item_id: "1124",  item: "Pant Small",description: "",},
];

我使用了map函数,但它不包括未修改的行

我的代码

  const test = convertedToJson.map((convertedToJson) => {
        if (!!convertedToJson.item.match(/Shirt/i)) {
            return convertedToJson.item + "Shirts are not available  ";
        }
    });
    console.log(test);

“我的输出”

const inventory = [
       'Shirts are not available small'
        'Shirts are not available big full'
    ];

【问题讨论】:

  • 这不是您运行代码时得到的输出。在item 中没有带有复数Shirts.match(/Shirts/i),因此,它返回一个undefineds 的数组。另外,如果要替换属性,请使用 forEach 而不是 map

标签: javascript node.js arrays json map-function


【解决方案1】:
const convertedInventory = inventory.map((it) => {
  const reg = new RegExp(/Shirt/i);
  if (it.item.match(reg)) {
    return {
      ...it,
      item: it.item.replace(reg, 'Shirts are not available')
    }
  }
  return it;
})

【讨论】:

  • 请在回答时添加一些上下文,这是一个很好的做法。
【解决方案2】:

您可以使用地图和替换

const inventory = [{
    line_item_id: "55412",
    item: "Shirt small",
    description: ""
  },
  {
    line_item_id: "55342",
    item: "shirt big full",
    description: ""
  },
  {
    line_item_id: "1124",
    item: "Pant Small",
    description: "",
  },
];

const newVal = inventory.map((elem) => {
  // checking if string contains shirt/Shirt
  if (elem.item.toLowerCase().indexOf('shirt') !== -1) {
    return {
      ...elem,
      // case-insensitive replacement
      item: elem.item.replace(/shirt/gi, "Shirts are not available")

    }
  } else {
    return { ...elem
    }
  }
});

console.log(newVal)

【讨论】:

    【解决方案3】:
    const test = convertedToJson.map((convertedToJson) => {
            if (convertedToJson.item.toLowerCase().indexOf('shirt') !== -1) {
                convertedToJson.item = convertedToJson.item.replace(/shirt/gi, "Shirts are not available");
            }
            return convertedToJson
        });
        console.log(test);
    

    您的代码的问题是只有在满足条件时才返回值。我只是将值返回到外面,如果满足“如果条件”,则将操纵该值

    【讨论】:

      【解决方案4】:

      您可以使用Array.prototype.map 轻松实现此目的。

      const inventory = [{
          line_item_id: "55412",
          item: "Shirt small",
          description: ""
        },
        {
          line_item_id: "55342",
          item: "shirt big full",
          description: ""
        },
        {
          line_item_id: "1124",
          item: "Pant Small",
          description: ""
        },
      ];
      
      const result = inventory.map((el) => {
        if (el.item.toLowerCase().includes("shirt")) {
          return { ...el,
            item: "Shirts are not available small"
          };
        }
        return el;
      });
      
      
      console.log( result );

      【讨论】:

        【解决方案5】:

        这边...

        const inventory = 
          [ { line_item_id: "55412", item: "Shirt small",    description: "" } 
          , { line_item_id: "55342", item: "shirt big full", description: "" } 
          , { line_item_id: "1124",  item: "Pant Small",     description: "" } 
          ] 
        
        inventory.forEach((obj,i,arr)=>
          arr[i].item = obj.item.replace(/shirt/i, 'Shirts are not available'))
        
        console.log( inventory )
        .as-console-wrapper {max-height: 100%!important;top:0;}

        【讨论】:

          猜你喜欢
          • 2016-05-26
          • 2019-04-29
          • 2018-05-21
          • 2022-07-29
          • 2021-04-22
          • 1970-01-01
          • 2020-04-12
          • 2014-07-04
          • 1970-01-01
          相关资源
          最近更新 更多