Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象属性从一个JSON数据文件添加到另一个JSON数据文件_Javascript_Arrays_Json_Angularjs - Fatal编程技术网

Javascript 将对象属性从一个JSON数据文件添加到另一个JSON数据文件

Javascript 将对象属性从一个JSON数据文件添加到另一个JSON数据文件,javascript,arrays,json,angularjs,Javascript,Arrays,Json,Angularjs,我有两个本地JSON文件。我想将一个文件中一个对象的属性添加到另一个文件中相应的对象 下面是一个例子 阵列1: [ { "id": 1, "username": "Joe Smith", "pic": "images/profile/Joe_smith.jpg" }, { "id": 2, "username": "Jane Smith", "pic": "images/profile/Jane_smith.jpg" } ] 阵

我有两个本地JSON文件。我想将一个文件中一个对象的属性添加到另一个文件中相应的对象

下面是一个例子

阵列1:

[
  {
    "id": 1,
    "username": "Joe Smith",
    "pic": "images/profile/Joe_smith.jpg"
  },
  {
    "id": 2,
    "username": "Jane Smith",
    "pic": "images/profile/Jane_smith.jpg"
  }
 ]
阵列2:

[
 {
   "id": 3,
   "userId": 1,
   "profession": "dentist"
 },
 { 
   "id": 4,
   "userId": 2,
   "profession": "pilot"
 }
其思想是将Array1中的pic属性添加到Array2中的正确对象。如果Array1中的id与Array2中的用户id匹配,则为正确匹配。Array2最终会变成这样:

[
 {
   "id": 3,
   "userId": 1,
   "profession": "dentist",
   "pic": "images/profile/Joe_smith.jpg"
 },
 { 
   "id": 4,
   "userId": 2,
   "profession": "pilot",
   "pic": "images/profile/Jane_smith.jpg"
 }

之后,我将使用angular来显示一个带有人脸的名称。希望我能解释清楚。任何帮助都将不胜感激

只是为了好玩。在本例中,使用


似乎与解析JSON、迭代两个数组并合并单个对象一样简单。你有什么特别的问题吗?你知道如何解析JSON吗?如何迭代数组?我知道如何迭代数组,在这种情况下,我可能需要重复两次。不过,解析对我来说还是新鲜事。谢谢你的费利克斯
var people = [
    { "id": 1, "username": "Joe Smith", "pic": "images/profile/Joe_smith.jpg" }, 
    { "id": 2, "username": "Jane Smith", "pic": "images/profile/Jane_smith.jpg"},
    { "id": 3, "username": "I play too much games", "pic": "images/profile/toomuch.jpg"}
];

var professions = [
    { "id": 3, "userId": 1, "profession": "dentist" },
    { "id": 4, "userId": 2, "profession": "pilot" }
];

var workers = _.map(people, function(human) {
    var work = _.findWhere(professions, { 'userId': human.id });
    return _.extend(human, work ? work : { 'profession' : 'unemployed' });
});

console.log(workers);