简体   繁体   English

从JavaScript中的对象数组中获取对象

[英]Getting object out of an array of objects in JavaScript

sorry I feel this is sort of a stupid question I have searched quite a bit and I cannot find my answer. 抱歉,我觉得这是一个愚蠢的问题,我已经搜索了很多,但找不到答案。

I have an array in JavaScript that I am inserting a bunch of elements. 我在JavaScript中有一个数组,正在插入一堆元素。 The trouble is I cannot then get the values of the elements back out, I think I am getting the property of the actual array. 麻烦的是我无法再获取元素的值,我想我正在获取实际数组的属性。 Below is what I am doing and what I am trying to do. 以下是我正在做的事情以及我想做的事情。 Thanks in advance for the help. 先谢谢您的帮助。 This is not the full traverse method, it actually does go through a bunch of times. 这不是完整的遍历方法,它确实经过了很多次。 When I previously just had an associative array items[a] it worked perfectly. 当我以前只有一个关联数组项目[a]时,它可以很好地工作。

var element = { html: "", group: "" };
var array = [];

function traverse(o) {
    for (var key in o) {
        element.html = "<li class='item'>" + key + " : </li>";
        element.group = b;
        array.push(element);
    }
}

I want to print the html from each element, the issue is I get the .html from only the first element each time. 我想从每个元素打印html,问题是我每次仅从第一个元素获取.html。

function printElements() {
    for (item in array) {
        var element = array.pop();
        console.log(element.html);
    }
}

I also tried. 我也试过了。

function printElements() {
    for (item in array) {
        console.log(array.html);
    }
}

I want to keep the array intact, I do not want to pop and elements I just want to print them. 我想保持阵列完整,我不想弹出,而我只想打印它们。

The main problem isn't to get the data out of the array, it never gets into the array in the first place. 主要的问题不是要从数组中取出数据,它永远不会首先进入数组。

You don't have a bunch of elements in the array, you only have one element that you change and add to the array over and over. 数组中没有一堆元素,只有一个元素可以更改并一遍又一遍地添加到数组中。 When you put it in the array it's not copied, it's just the reference to the element that is placed in the array. 当您将其放置在数组中时,它不会被复制,而只是对放置在数组中的元素的引用。 You end up with an array full of references to the same single element. 您最终得到一个数组,其中包含对同一单个元素的引用。

Create a new element for each iteration in the loop when you populate it: 填充循环时,为循环中的每个迭代创建一个新元素:

for (var key in o) {
  var element = {};
  element.html = "<li class='item'>" + key + " : </li>";
  element.group = b;
  array.push(element);
}

Use a simple for loop instead: 使用简单的for循环:

for (var i = 0; i < array.length; i++) {
    var item = array[i];
    // do stuff
}

It is unadvisable to use the nicer for loop syntaxes on arrays in JavaScript because they will iterate through all key/value pairs in the array, not just the array numeric keys. 不建议在JavaScript中的数组上使用更佳的for循环语法,因为它们将遍历数组中的所有键/值对,而不仅仅是数组数字键。 This is because arrays are objects, too, and the for...in loop syntax does not differentiate between them. 这是因为数组也是对象,并且for ... in循环语法无法区分它们。

You're not using the iterated object, instead you are using array... change this: 您不使用迭代对象,而是使用数组...更改此方法:

function printElements() {
    for (item in array) {
        console.log(array.html);
    }
}

to this: 对此:

function printElements() {
    for (item in array) {
        console.log(array[item].html);
    }
}

jsfiddle jsfiddle

Do it this way: 这样做:

var array = [];

function traverse(o) {
    for (var key in o) {
        var b = 'b'; // if that is what you have ment
        var element = {  html : "<li class='item'>" + key + " : </li>", group : b};
        array.push(element);
    }
}

function printElements() {
    for (var item=0; item<array.length; item++) {
        console.log(array[item].html);
    }
}

That's because it isn't safe to use for each in case of arrays and you should have new object for each array element. 这是因为在使用数组的情况下,每种方法都不安全,并且每个数组元素都应有一个新对象。 Be sure not to use delete on array elements either. 确保也不要在数组元素上使用delete。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM