如何将一个对象附加到另一个对象?

9
有人能帮我将一个对象附加到另一个对象上吗?例如,我有原始对象:
var object = {
    { product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
    }
}

我希望您能添加以下内容:
    { product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }

所以最终,object看起来像:

object = {
    { product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
    },
    { product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
}

我该怎么做?


2
var object = { {key: "value"} } 不是有效的 JavaScript。对象属性需要键。 - Explosion Pills
你的问题中没有JSON。JSON是一种文本表示法。如果你正在处理JavaScript源代码,并且不是在处理字符串,那么你就不是在处理JSON。 - T.J. Crowder
使用对象数组,你正在尝试使用对象中的对象,这将会变得更加复杂,并且你当前的结构不支持它。 - Rajshekar Reddy
7个回答

13
你可以使用ES6扩展运算符来追加对象。
object = { ...object, 
...{ product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
}

8
你的对象变量应该是一个数组,而不是一个对象。
    var object = [
        { product_code: '55993',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '300.00',
          quantity: '3' 
        }
    ]

然后,您可以将其他对象添加到其中:
object.push({ product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    });

希望这能有所帮助。

3

您有几个选择:

  1. Define a property name under which to store the second object, which won't give you a structure like the one you've shown at the end of your question.

  2. Use an array, which will give you something very nearly like what you have at the end of your question:

    [
        { product_code: '55993',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '300.00',
          quantity: '3' 
        },
        { product_code: '90210',
          brand: 'Phillips66',
          category: 'LUBES AND GREASES',
          description: 'cuatro cinco seis',
          price: '500.00',
          quantity: '5' 
        }
    ]
    

    Note the [ and ] instead of { and } at the outermost level.

如果您有一个对象:

var object1 = {foo: "bar"};

还有另一个对象:

var object2 = {biz: "baz"};

那么选项#1看起来像这样:
object1.anyNameHere = object2;

...这将为您提供:

{
   foo: "bar",
   anyNameHere: {
       biz: "baz"
   }
}

或者选项#2:
var a = [object1, object2];

...这将为您提供:

[
    {
        foo: "bar"
    },
    {
        biz: "baz"
    }
]

1

有两种方法,但第一种是最好的方法,您可以使用for循环轻松检索数据。

var object1 = { product_code: '55993',
    brand: 'Phillips66',
    category: 'LUBES AND GREASES',
    description: 'cuatro cinco seis',
    price: '300.00',
    quantity: '3'
};

var object2 ={ product_code: '55993',
    brand: 'Phillips66',
    category: 'LUBES AND GREASES',
    description: 'cuatro cinco seis',
    price: '300.00',
    quantity: '3'
}

创建一个数组变量,并将所有对象推入该数组。
var myObjectList = [];
myObjectList.push(object1);
myObjectList.push(object2);

2) 您可以创建对象并为该对象分配值。
var myObjectList = {object1: object1, object2 : object2};

1

使用 Object.assign( object, tail ){ ...object, ...tail },如 [1] 中所述:

当您要合并一个可变的 var 时,assign... 看起来会像这样:

var object = {
      product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
};

object = Object.assign( object, { product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
);

or

object = { ...object, ...{ product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
    }
};


如果您想合并两个 const不可变对象,您需要使用assign...操作符创建一个新的对象:
const object = {
      product_code: '55993',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '300.00',
      quantity: '3' 
};

const tail = {
      product_code: '90210',
      brand: 'Phillips66',
      category: 'LUBES AND GREASES',
      description: 'cuatro cinco seis',
      price: '500.00',
      quantity: '5' 
};

const result = Object.assign( object, tail );

or

const result = { ...object, ...tail };

请注意,您可以使用更多参数链接assign / ...。您还可以使用它来相应地连接{{xx}}对象(假设它们已经是有效的对象)。
这是我想要使用一些基本根参数列表扩展我的数据模型,但我不想使用class的东西 :-)
[1] https://attacomsian.com/blog/javascript-merge-objects

0

如果你的字段不同:

<body>

<p id="p1">This is a paragraph.</p>

<script>
var primaryValue = {"id": "4"};

var obj = new Object();
obj = Object.assign(primaryValue,obj);
obj['name'] = 'sara';
obj['age'] = '32';

document.getElementById("p1").innerHTML = obj['id'] + ", " + obj['name'] + ", " + obj['age'];
</script>

</body>

输出结果为:

4, sara, 32

0

我赞同CaptainAdmin使用扩展语法的回答。非常有用,知道它的存在很好。然而,在这种情况下,更方便和正确的做法是使用类似以下的内容:

var o1 = {
    product_name: "Super One",
};

var o2 = {
    quantity: 50,
};

specializeWith(o1, o2);

在哪里:

function specialize_with(o, S) { for (var prop in S) { o[prop] = S[prop]; } }

那么你的o1将会是:

var o1 = {
    product_name: "Super One",
    quantity: 50,
};

不幸的是,仔细查看原始问题,我发现一开始并不是这样要求的。需要的是对象数组,而不是继承、专业化或扩展。不过,还是留在这里吧。

另请参见 https://github.com/latitov/OOP_MI_Ct_oPlus_in_JS,其中的 specializeWith() 就出自此处。

请再次查看 https://github.com/latitov/OOP_MI_Ct_oPlus_in_JS


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接