Javascript 在数据数组和对象的合并中,如何使用对象赋值而不是扩展语法?
我使用扩展语法来获取当前对象Javascript 在数据数组和对象的合并中,如何使用对象赋值而不是扩展语法?,javascript,arrays,object,spread-syntax,Javascript,Arrays,Object,Spread Syntax,我使用扩展语法来获取当前对象 const x = [{ port: 3000, typ: "port" }, { port: 4000, typ: "port" }]; const IDs = [3246237348, 738423894, 73824923] const y = { ...x[0], CSSID }; Object {port: 3000, typ: "port", CSSID: Array[3]} port: 3000 typ: "port" CSSID
const x = [{ port: 3000, typ: "port" }, { port: 4000, typ: "port" }];
const IDs = [3246237348, 738423894, 73824923]
const y = {
...x[0],
CSSID
};
Object {port: 3000, typ: "port", CSSID: Array[3]}
port: 3000
typ: "port"
CSSID: Array[3]
0: 3246237348
1: 738423894
2: 73824923
但是我想使用object assign而不是spread语法,看起来很简单,但我不知道如何得到结果:
const ob = Object.assign(Object.assign({}, x[0], Object.assign(CSSID)) );
Object {0: 3246237348, 1: 738423894, 2: 73824923, port: 3000, typ: "port"}
0: 3246237348
1: 738423894
2: 73824923
Object.assign()
将属性从一个或多个对象复制到单个目标对象。由于CSSID
是一个数组,它将数组的属性(项)复制到对象。由于需要具有属性CSSID的对象,请将其设置为目标对象的属性,或设置为以下源之一:
CSSID应该是对象的属性:
const x=[{port:3000,典型:“port”},{port:4000,典型:“port”}];
常量CSSID=[32462373348,738423894,73824923];
const ob=Object.assign({},x[0],{CSSID});//赋值({CSSID},x[0]);
控制台日志(ob)
Object.assign()
将属性从一个或多个对象复制到单个目标对象。由于CSSID
是一个数组,它将数组的属性(项)复制到对象。由于需要具有属性CSSID的对象,请将其设置为目标对象的属性,或设置为以下源之一:
CSSID应该是对象的属性:
const x=[{port:3000,典型:“port”},{port:4000,典型:“port”}];
常量CSSID=[32462373348,738423894,73824923];
const ob=Object.assign({},x[0],{CSSID});//赋值({CSSID},x[0]);
控制台日志(ob)代码>