javascript - 如何获取数组中的 JSON 对象?

标签 javascript jquery angularjs arrays json

var data =  '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

这里我发布了我的 JSON 数据。我想获取 JSON/Array 格式的所有 defaultValue。我的输出应该是这样的 -

 defaultValues:['Size30','Black','Low']

如何在 foreach 循环中管理它?

我的代码:

var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
      if (optionValue.defaultValue.text) {
          otherSelectedOption = (optionValue.defaultValue.text);
      }
      selectedOption = {defaultValues: otherSelectedOption};
      console.log(selectedOption);          
});

最佳答案

您的 JSON 无效,因为对象未用逗号分隔 ,

假设这是 JSON

var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';

尝试

var arr = JSON.parse(obj).map( function(item){
   return item.defaultValue;
});

关于javascript - 如何获取数组中的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930715/

相关文章:

javascript - 如何将字段名称传递给 Mongoose 的 update() 方法?

javascript - 将嵌套 knockout View 模型解析为 JSON 时出现问题

javascript - javascript中的触发键

php - 使用 php 和 jquery 将输入从表单插入到数据库

javascript - Angular Promise 回调不在构造函数方法内触发,而是在对象字面量方法中触发

javascript - 如何根据变量值动态请求 React 组件?

javascript - 原型(prototype)继承应该节省内存吧?

javascript - 如何创建更好的方法来维护基于我的代码访问的最后一页

javascript - 是什么导致 Firebase 应用中出现 "Timer Installed"或 "Timer removed"?

c# - 使用 ASP.NET MVC 4 和 Angularjs 的异步 Web 应用程序