通过在JSON数据中查找值来创建新的对象数组

时间:2017-10-17 15:25:10

标签: javascript arrays json object

我不确定我能做到这一点,但我想要做的是通过搜索一些JSON数据来创建一个新数组,以找到一个键,并在找到密钥时将整个对象带入新数组。以下是我正在使用的数据样本。

{
  "students": {
    "AB10001": {
      "campus": "cda",
      "subjects": ["history", "english"],
    }
    "AB10002": {
      "campus": "asd",
      "subjects": ["maths"],
    }
    "AB10003": {
      "campus": "asd",
      "subjects": ["english"],
    }
    "AB10004": {
      "campus": "asd",
      "subjects": ["history"],
    }
    "AB10005": {
      "campus": "cda",
      "subjects": ["maths", "science"],
    }
    "AB10006": {
      "campus": "asd",
      "subjects": ["science"],
    }
    "AB10007": {
      "campus": "cda",
      "subjects": ["science"],
    }
    "AB10008": {
      "campus": "asd",
      "subjects": ["science", "history"],
    }
    "AB10009": {
      "campus": "cda",
      "subjects": ["history"],
    }
    "AB10010": {
      "campus": "cda",
      "subjects": ["history", "maths"],
    }
  }
}

所以我要做的是为所有subjects的学生搜索history的学生键,然后从中创建另一个数组,取整个学生,同时保留原始对象相同。

所以我想结束这样的事情:

{
  "historyStudents": {
    "AB10001": {
      "campus": "cda",
      "subjects": ["history", "english"],
    }
    "AB10004": {
      "campus": "asd",
      "subjects": ["history"],
    }
    "AB10008": {
      "campus": "asd",
      "subjects": ["science", "history"],
    }
    "AB10009": {
      "campus": "cda",
      "subjects": ["history"],
    }
    "AB10010": {
      "campus": "cda",
      "subjects": ["history", "maths"],
    }
  }
}

任何见解或帮助都会非常有帮助。提前谢谢!

4 个答案:

答案 0 :(得分:2)

您可以使用reduce()history创建只有var data = {"students":{"AB10001":{"campus":"cda","subjects":["history","english"]},"AB10002":{"campus":"asd","subjects":["maths"]},"AB10003":{"campus":"asd","subjects":["english"]},"AB10004":{"campus":"asd","subjects":["history"]},"AB10005":{"campus":"cda","subjects":["maths","science"]},"AB10006":{"campus":"asd","subjects":["science"]},"AB10007":{"campus":"cda","subjects":["science"]},"AB10008":{"campus":"asd","subjects":["science","history"]},"AB10009":{"campus":"cda","subjects":["history"]},"AB10010":{"campus":"cda","subjects":["history","maths"]}}} var result = { historyStudents: Object.keys(data.students) .reduce(function(r, e) { if(data.students[e].subjects.includes('history')) r[e] = data.students[e] return r; }, {}) } console.log(result)主题的学生的新对象。



{{1}}




答案 1 :(得分:1)

您只需使用data.students循环遍历Object.keys()密钥,并检查每个密钥是否history数组中的相对对象值subjetcs

var result = {
  "historyStudents": {}
};

Object.keys(data.students).forEach(function(k) {
  if (data.students[k].subjects.indexOf("history") > -1) {
    result.historyStudents[k] = data.students[k];
  }
});

<强>演示:

var data = {
  "students": {
    "AB10001": {
      "campus": "cda",
      "subjects": ["history", "english"],
    },
    "AB10002": {
      "campus": "asd",
      "subjects": ["maths"],
    },
    "AB10003": {
      "campus": "asd",
      "subjects": ["english"],
    },
    "AB10004": {
      "campus": "asd",
      "subjects": ["history"],
    },
    "AB10005": {
      "campus": "cda",
      "subjects": ["maths", "science"],
    },
    "AB10006": {
      "campus": "asd",
      "subjects": ["science"],
    },
    "AB10007": {
      "campus": "cda",
      "subjects": ["science"],
    },
    "AB10008": {
      "campus": "asd",
      "subjects": ["science", "history"],
    },
    "AB10009": {
      "campus": "cda",
      "subjects": ["history"],
    },
    "AB10010": {
      "campus": "cda",
      "subjects": ["history", "maths"],
    }
  }
};
var result = {
  "historyStudents": {}
};

Object.keys(data.students).forEach(function(k) {
  if (data.students[k].subjects.indexOf("history") > -1) {
    result.historyStudents[k] = data.students[k];
  }
});
console.log(result);

答案 2 :(得分:0)

您可以获取密钥,过滤密钥并从密钥和值中构建新对象。然后使用Object.assign创建单个对象。

&#13;
&#13;
function getStudents(object, subject) {
    return Object.assign({}, ...Object
        .keys(object)
        .filter(k => object[k].subjects.includes(subject))
         .map(k => ({ [k]: object[k] }))
    );
}


var object = { students: { AB10001: { campus: "cda", subjects: ["history", "english"] }, AB10002: { campus: "asd", subjects: ["maths"] }, AB10003: { campus: "asd", subjects: ["english"] }, AB10004: { campus: "asd", subjects: ["history"] }, AB10005: { campus: "cda", subjects: ["maths", "science"] }, AB10006: { campus: "asd", subjects: ["science"] }, AB10007: { campus: "cda", subjects: ["science"] }, AB10008: { campus: "asd", subjects: ["science", "history"] }, AB10009: { campus: "cda", subjects: ["history"] }, AB10010: { campus: "cda", subjects: ["history", "maths"] } } },
    result = { historyStudents: getStudents(object.students, 'history') };

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

除上述答案外,您还可以使用es6 destructuring来获得所需的结果

const  data = {"students":{"AB10001":{"campus":"cda","subjects":["history","english"]},"AB10002":{"campus":"asd","subjects":["maths"]},"AB10003":{"campus":"asd","subjects":["english"]},"AB10004":{"campus":"asd","subjects":["history"]},"AB10005":{"campus":"cda","subjects":["maths","science"]},"AB10006":{"campus":"asd","subjects":["science"]},"AB10007":{"campus":"cda","subjects":["science"]},"AB10008":{"campus":"asd","subjects":["science","history"]},"AB10009":{"campus":"cda","subjects":["history"]},"AB10010":{"campus":"cda","subjects":["history","maths"]}}}

const historyStudents = {};
for(const stdId in data.students) {

  // es6 destructuring
  const { subjects } = data.students[stdId];

  if(subjects.includes('history')){
    historyStudents[stdId] = data.students[stdId]; 
  }
}
// object property shorthand
console.log({ historyStudents });