将嵌套json属性的值分配给另一个复杂的json对象

时间:2018-07-27 03:40:53

标签: javascript javascript-objects

我想将javascript对象的每个字段分配给另一个对象的字段:

对象1:

from os import listdir
from os.path import isfile, join
m = '/' # your path
files_in_directory = [f for f in listdir(m) if isfile(join(m, f))]
files = pd.Series(files_in_directory)

s = pd.Series(["Granny Smith Apple",
"High Quality Orange",
"Delicious soup"])

z = pd.Series(s.str.split().sum())
files.str.contains('|'.join(z))

对象2

我的另一个对象就像

[{
  'field1': 'somevalue'
}, {
  'field2': 'somevalue'
}, {
  'field3': [{
      'field3_1': 'somevalue'
    },
    {
      'field3_2': 'somevalue'
    }
  ]
}]

如何使用javascript实现?当我尝试以下操作时:

{
    "field1": "value1",
    "field2": "value2",
    "field3": "value 3",
    "field4": "value4",
    "field4": value5,
}

我收到ReferenceError:未定义field1

预期输出:

object1.field1=object2.field1
object1.field3.field3_1=object2.field3

1 个答案:

答案 0 :(得分:1)

首先,有一个未声明的对象value5,它可能是字符串,然后必须放置“”。然后,您可以按照方法

let object1={};
let object2 ={
    "field1": "value1",
    "field2": "value2",
    "field3": "value 3",
    "field4": "value4",
    "field4": "value5",
};

object1.field1 =object2.field1;
object1.field3=[];
object1.field3.field3_1=object2.field3
console.log(object1);

如果不声明父对象,则无法分配值。希望这就是您要寻找的。