【问题标题】:How to assign values by property in nested object array using javascript如何使用javascript在嵌套对象数组中按属性分配值
【发布时间】:2019-04-24 07:40:53
【问题描述】:

我想知道如何在javascript中通过id=insta为嵌套对象中的对象属性赋值

我有两个对象,我需要使用 javascript 将一个对象属性应用到另一个对象

我被卡住了,不知道如何继续,

obj1.forEach(e=> {if(e.id==='insta') Object.assign(e, obj2)})
var obj1 = [
  {
    id: "insta",
    rate: "2.4",
    fee: "0",
    amount: "400"
  },
 {
    id: "trans",
    rate: "1.4",
    fee: "0",
    amount: "200"
  }
]

var obj2 = 
{
  data: {
     rate_value: "4.4",
     fee_value: "10",
     targetamount: "5000",
     country_code: "SG"
   }
}

Expected Output: 

res= [
  {
    id: "insta",
    rate: "4.4",
    fee: "10",
    amount: "5000",
    country_code: "SG"
  }
]

【问题讨论】:

  • 您希望 obj2 的所有属性都在 res 对象中还是仅在 country_code 中?
  • 如何映射属性?这看起来不像是一个合理的标准。 rate_value 变为 rate,而 targetamount 变为 amount?

标签: javascript jquery arrays html object


【解决方案1】:

正如您预期的输出所示,您只想要id="insta" 的项目,因此使用filter() 来获取这些项目。然后使用map() 并在地图内创建一个临时对象。并使用扩展运算符返回组合对象。

注意:您需要创建另一个对象,因为obj2 中的属性名称和数组不同。

var obj1 = [ { id: "insta", rate: "2.4", fee: "0", amount: "400" }, { id: "trans", rate: "1.4", fee: "0", amount: "200" }]
var obj2 = { data: { rate_value: "4.4", fee_value: "10", targetamount: "5000", country_code: "SG" } }

const res = obj1.filter(x => x.id === "insta").map(x => {
  const {data} = obj2
  let temp = {
    rate : data.rate_value,
    fee : data.fee_value,
    amount : data.targetamount,
    country_code : data.country_code
  }
  return {...x,...temp}
})


console.log(res)

【讨论】:

    【解决方案2】:

    我们可以使用reduce 方法将数组缩减为我们想要的结果。在这里,我在reduce 方法的回调中添加if 条件和来自obj2 的映射值。基本上过滤和映射已经完成,在reduce回调方法中。

    var obj1 = [{
        id: "insta",
        rate: "2.4",
        fee: "0",
        amount: "400"
      },
      {
        id: "trans",
        rate: "1.4",
        fee: "0",
        amount: "200"
      }
    ]
    
    var obj2 = {
      data: {
        rate_value: "4.4",
        fee_value: "10",
        targetamount: "5000",
        country_code: "SG"
      }
    }
    
    const result = obj1.reduce((acc, curr) => {
      if (curr.id === 'insta') {
        acc.push({
          ...curr,
          rate: obj2.data.rate_value,
          fee: obj2.data.fee_value,
          amount: obj2.data.targetamount,
          country_code: obj2.data.country_code
        })
      }
      return acc;
    }, []);
    
    console.log(result);

    【讨论】:

    • @brk 也许是因为只是回答了问题而没有解释。博尼 你应该解释你写的解决方案。也许您的解决方案是正确的,并且会帮助 OP。但不能帮助其他人稍后看到答案。
    • @Boney 用我会赞成的某种解释更新答案。
    • 谢谢。我已经添加了解释。
    【解决方案3】:

    首先,您可以使用Array.filter 将数组包含在id = "insta" 中的对象,然后使用Array.mapobj2 中的数据应用于每个项目。

    类似的东西:

    var obj1 = [{
        id: 'insta',
        rate: '2.4',
        fee: '0',
        amount: '400',
      },
      {
        id: 'trans',
        rate: '1.4',
        fee: '0',
        amount: '200',
      },
    ];
    
    var obj2 = {
      data: {
        rate_value: '4.4',
        fee_value: '10',
        targetamount: '5000',
        country_code: 'SG',
      },
    };
    
    const result = obj1
      .filter(item => item.id === 'insta')
      .map(item => ({
        id: item.id,
        rate: obj2.data.rate_value,
        fee: obj2.data.fee_value,
        amount: obj2.data.targetamount,
        country_code: obj2.data.country_code,
      }));
      
      console.log(result)

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 2017-04-11
      • 2022-01-04
      • 2021-05-09
      • 2019-07-29
      • 2018-01-02
      • 2013-10-11
      • 2023-02-07
      相关资源
      最近更新 更多