替换对象中的数组项

Replace an array items in an object

本文关键字:数组 对象 替换      更新时间:2023-09-26

我有一个像下面这样的对象。在propertymixed数组中,我有一个数组,我想用ch_array项替换数组。

var myObject = {
    propertyString: "this is a string",
    propertyAnotherString: "this is another string",
    propertyMixedArray: [{"item 1"},{ "item 2"}, {"item 3"}],  
    propertyObject: { someProperty: "and so on..." }
};
ch_array =[{"new val1"},{ "new val 2"}, {"newval3"}];
var myObject = {
    propertyString: "this is a string",
    propertyAnotherString: "this is another string",
    propertyMixedArray: [{"new val1"},{ "new val 2"}, {"newval3"}],  
    propertyObject: { someProperty: "and so on..." }
};

将属性赋给新数组:

myObject.propertyMixedArray = ch_array;