返回带有筛选结果的新JSON对象
Returning a new JSON object with filtered results
我希望在过滤一些结果后,从现有的JS对象返回一个新的JS对象。
假设我有以下JS对象:
{ "Examples" :
[ { "id": 1, "name": "Tim", "answerswer": "yes"},
{ "id": 2, "name": "Jon", "answerswer": "no"},
{ "id": 3, "name": "Don", "answerswer": "yes" ] }
我想通过这个对象过滤所有的是答案,并返回一个看起来像这样的对象:
{ "Examples" :
[ { "id" : 1, "name": "Tim", "answerswer": "yes"},
{ "id" : 3, "name": "Don", "answerswer": "yes"} ] }
首先:这是一个JavaScript对象,而不是JSON对象。JSON是一种序列化方案。
您可以使用.filter()
:过滤阵列
var obj = { "Examples" :
[ { "id": 1, "name": "Tim", "answerswer": "yes"},
{ "id": 2, "name": "Jon", "answerswer": "no"},
{ "id": 3, "name": "Don", "answerswer": "yes" ] };
obj.Examples = obj.Examples.filter(function(entry) {
return entry.answer === "yes";
});
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象