为了账号安全,请及时绑定邮箱和手机立即绑定

如何将单个键值对对象转换为具有键值对的对象数组?

如何将单个键值对对象转换为具有键值对的对象数组?

千万里不及你 2023-03-24 16:26:28
我有一个键值对的单个对象,每个键都有一个数组作为值,如何通过将它与另一个对象数组进行比较,将其转换为具有键值对属性的对象数组。var keys = [];for (var k in obj) keys.push(k);输入:obj =  {          "name": "jack",          "age": 10,          "country": "india",          "state": "Delhi"        }        obj2 = [{key: "product", type:"", value:"", required:"true", mandatory:"true"},        {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"},        {key: "name", type:"text", value:"", required:"true", mandatory:"true"},        {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"},        {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"},        {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}]预期输出:result = [{key: "name", type:"text", value:"", required:"true", mandatory:"true",setValue:"jack"},        {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true",setValue:"10"},        {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true",setValue:"india"},        {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true",setValue:"Delhi"}];
查看完整描述

3 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

循环结束obj2。如果该key值是 的属性obj,则将相应的值复制到setValue对象的属性中,并将其压入结果。


obj = {

  "name": "jack",

  "age": 10,

  "country": "india",

  "state": "Delhi"

};

obj2 = [{key: "product", type="", value="", required="true", mandatory="true"},

        {key: "sub-product", type="", value="", required="true", mandatory="true"},

        {key: "name", type="text", value="", required="true", mandatory="true"},

        {key: "age", type="text", value="[0-9]", required="true", mandatory="true"},

        {key: "country", type="text", value="[a-z]", required="true", mandatory="true"},

        {key: "state", type="text", value="[a-z]", required="true", mandatory="true"}];


var result = [];

obj2.forEach(o => {

  if (o.key in obj) {

    o.setValue = obj[o.key];

    result.push(o);

  }

});

console.log(result);


查看完整回答
反对 回复 2023-03-24
?
LEATH

TA贡献1936条经验 获得超6个赞

您可以先使用过滤它hasOwnProperty,然后将其映射到设置setValue属性。这是实现:


var obj = { "name": "jack", "age": 10, "country": "india", "state": "Delhi" };

var obj2 =[{key: "product", type:"", value:"", required:"true", mandatory:"true"}, {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"}, {key: "name", type:"text", value:"", required:"true", mandatory:"true"}, {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"}, {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"}, {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}];



var result = obj2.filter(k=>obj.hasOwnProperty(k.key)).map(p=>({...p, setValue:obj[p.key]}));


console.log(result);


查看完整回答
反对 回复 2023-03-24
?
撒科打诨

TA贡献1934条经验 获得超2个赞

你可以使用Array.prototype.flatmap来实现这个

const obj = {

  "name": "jack",

  "age": 10,

  "country": "india",

  "state": "Delhi"

}

const obj2 = [

  {key: "product", type:"", value:"", required:"true", mandatory:"true"},

  {key: "sub-product", type:"", value:"", required:"true", mandatory:"true"},

  {key: "name", type:"text", value:"", required:"true", mandatory:"true"},

  {key: "age", type:"text", value:"[0-9]", required:"true", mandatory:"true"},

  {key: "country", type:"text", value:"[a-z]", required:"true", mandatory:"true"},

  {key: "state", type:"text", value:"[a-z]", required:"true", mandatory:"true"}

]


const result = obj2.flatMap((entry) => {

  if (obj[entry.key]) {

    return {

      ...entry,

            setValue: obj[entry.key],

    }

  }

    return []

})


console.log(result)



查看完整回答
反对 回复 2023-03-24
  • 3 回答
  • 0 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信