【问题标题】:Transfer value of object inside array to another array将数组内对象的值转移到另一个数组
【发布时间】:2017-01-15 10:18:30
【问题描述】:

我有一个这样的对象:

var animals_data = {
category : [
    {
        class : "Reptiles",
        animals : [
            {
                image1 : "some image",
                image2 : "some image 2",
                name : "Snake",
                description : "some description"
            },
            {
                image1 : "another image",
                image2 : "another image 2",
                name : "Crocodilia",
                description : "another description"
            },
        ]
    },
    {
        class : "Mammals",
        animals : [
            {
                image1 : "more image",
                image2 : "more image 2",
                name : "Cat",
                description : "more description"
            },
            {
                image1 : "image",
                image2 : "image 2",
                name : "Dog",
                description : "long description"
            }
        ]
    },
    {
        class : "Birds",
        animals : [
            {
                image1 : "bird image",
                image2 : "bird image 2",
                name : "American flamingo",
                description : "bird description"
            },
            {
                image1 : "another bird image",
                image2 : "another bird image 2",
                name : "Atlantic puffin",
                description : "another bird description"
            },
        ]
    }

]};

我想获取 firstIndex 的值并将它们放在不同的数组中。但是,我很难做到这一点。我做的代码是:

for (var i = 0; i < animals_data.category.length; i++) {
var currentIteration = animals_data.category[i];    
var currentClass = currentIteration.class;

animals_array.push(currentClass);
};

当我运行代码时,错误提示:无法读取未定义的属性“firstIndex”。感谢您的帮助。

编辑:包括我正在使用的实际对象。我省略了一些部分,但基本上,这就是要点。我要放入新数组的值是“类”属性中的值。

【问题讨论】:

  • 您的对象字面量有问题。请构建一个工作示例。
  • 当前代码产生Uncaught SyntaxError: Unexpected token ]
  • for 语句的末尾删除;i++){ 之间应该只有空格。就目前而言,您会立即完成循环。
  • 编辑它以包含我正在使用的对象。
  • 您的示例仍然无法重现。 Uncaught ReferenceError: obj is not defined

标签: javascript arrays data-structures mapping javascript-objects


【解决方案1】:

解决了。感谢 Xotic750 指出这个错误。我的问题是:

当我使用时:

for (var i = 0; i < obj.firstKey.length; i++); {
var currentIteration = obj.firstKey[i];
var currentIndex = currentIteration.firstIndex;

new_array.push(currentIndex);
};

数组加倍(即每个添加两个)。

但是当我用它替换它时

new_array.push(obj.firstKey[i].firstIndex;

它运行正确。我可以知道为什么会有这样的差异吗?谢谢。

【讨论】:

  • 两种方法的结果应该没有区别。但是没有人确切知道会发生什么,因为您提供的数据结构和处理某些(其他)数据的代码不匹配。 >>...它运行正确...for (var i = 0; i < obj.firstKey.length; i++); { ...-最后一个分号将阻止按预期运行循环; (b) new_array.push(obj.firstKey[i].firstIndex; - 缺少右括号。
【解决方案2】:

必须对提供的数据结构和任何列表结构进行清理 那就是成员只需要从一种类型/形式转换为另一种, 由map 处理最好。

var
    animalData = {
        categories: [{

            class   : "Reptiles",
            animals : [{

                image1 : "some image",
                image2 : "some image 2",
                name : "Snake",
                description : "some description"
            }, {
                image1 : "another image",
                image2 : "another image 2",
                name : "Crocodilia",
                description : "another description"
            }]

        }, {
            class   : "Mammals",
            animals : [{

                image1 : "more image",
                image2 : "more image 2",
                name : "Cat",
                description : "more description"
            }, {
                image1 : "image",
                image2 : "image 2",
                name : "Dog",
                description : "long description"
            }]

        }, {
            class   : "Birds",
            animals : [{

                image1 : "bird image",
                image2 : "bird image 2",
                name : "American flamingo",
                description : "bird description"
            }, {
                image1 : "another bird image",
                image2 : "another bird image 2",
                name : "Atlantic puffin",
                description : "another bird description"
            }]
        }]
    },

    animalClassNameList = animalData.categories.map(function (category) {
        return category.class;
    });


console.log("animalData : ", animalData);
console.log("animalClassNameList : ", animalClassNameList);

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 2018-02-05
    • 2021-07-09
    • 2022-11-17
    • 2021-12-13
    • 2020-09-09
    • 2021-09-03
    • 2011-08-31
    相关资源
    最近更新 更多