【问题标题】:Convert object into an array Angular将对象转换为数组 Angular
【发布时间】:2019-05-17 12:24:42
【问题描述】:

我是 javascript 和 Angular 的新手。假设我有一个带有 {"xyz":{Key:info}} 的 JSON 对象。我想将 {Key:info} 添加到数组中。

我想让 "xyz" 成为一个数组。例如:{"xyz":[{Key:info}]} 这样我就可以将更多的{Key:info} 推入该数组-{"xyz":[{Key:info},{Key:info},{Key:info}]}

我还需要每次检查 xyz 是否为对象,然后将其设为数组并仅推送一次。

我不知道如何使用 Angular javascript 做到这一点。

编辑:- 添加原始 JSON

$scope.ContentObj= {
      "attribute-set": [
        {
          "attribute": [
            {
              "_name": "text-align",
              "__prefix": "xsl",
              "__text": "end"
            },
            {
              "_name": "end-indent",
              "__prefix": "xsl",
              "__text": "10pt"
            }
          ],
          "_name": "odd__header",
          "__prefix": "xsl"
        },
        {
          "attribute": {
            "_name": "font-weight",
            "__prefix": "xsl",
            "__text": "bold"
          },
          "_name": "pagenum",
          "__prefix": "xsl"
        }
      ],
      "_version": "2.0",
      "__prefix": "xsl"
    }

【问题讨论】:

    标签: javascript arrays angularjs json


    【解决方案1】:

    这可能是你要问的。

    if (!angular.isArray($scope.yourObject.xyz)){
        $scope.yourObject = {
            xyz: []
        }
    }
    $scope.yourObject.xyz.push({Key:'info'})
    

    【讨论】:

    • 我做到了,但现在我的数组被推送了两次,例如{"xyz":[[{Key:info},{Key:info},{Key:info}],{Key:info},{Key:info}]}
    • 但我认为,每当控制器每次加载时,这段代码都会创建数组。我只想找到一次对象。如果数组存在则跳过。
    【解决方案2】:

    您可以使用typeof 知道它是否是一个对象,然后您可以获取它的内容并用它初始化一个数组

    let myObject = {"xyz":{Key:"info"}};
    
    if(typeof myObject.xyz === "object"){ //Check if object
      const content = myObject.xyz; //Get the content
      myObject.xyz = [content]; //Put the content in an array
    }
    
    console.log(myObject);

    如果您需要按照 cmets 的要求在代码中使用它:

    if(typeof $scope.ContentObj.stylesheet["attribute-set"][4].xyz === "object"){ //Check if object
      const content = $scope.ContentObj.stylesheet["attribute-set"][4].xyz; //Get the content
      $scope.ContentObj.stylesheet["attribute-set"][4].xyz = [content]; //Put the content in an array
    }
    
    console.log(myObject);
    

    【讨论】:

    • 嗨,感谢您提供干净的代码。现在假设我需要以这种方式调用信息 let myObject = $scope.ContentObj.stylesheet["attribute-set"][4].xyz; ,然后在控制台中我只能看到 object 。
    • 然后将myObject替换为$scope.ContentObj.stylesheet["attribute-set"][4]并保留第一行初始化对象
    • 你的意思是说对象let myObject = {"xyz":{Key:"info"}};应该是硬编码的?
    • 我仍然对第一行感到困惑。 let myObject 。我在 EDIT 中添加了我真正的 JSON 调用,我需要让它像第一个 attribute-set 一样。致电scope.ContentObj.stylesheet["attribute-set"][1]。这里可以看到attribute-set[1].attribute是对象。
    • 只需将typeof myObject === .. 替换为typeof $scope.ContentObj.stylesheet["attribute-set"][1] === ... 并对以下行执行相同操作。您还必须删除第一行let myObject = ...。我看到您缺少 javascript 的基础知识。您应该首先阅读有关基本 javascript 的教程
    【解决方案3】:

    试试这个方法。

    $scope.yourObject.xyz.push(Object.assign([], yourObject))
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2018-12-04
      • 2021-06-16
      相关资源
      最近更新 更多