如何解决Javascript对象会覆盖previouse对象的问题?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (162)

因此,im试图用javascript废弃html有困难,我添加到数组中的每一个新对象都会更改以前添加的每个对象:

$.get("Readfrom.php", null, function(html) {
  //console.log(html);
  var data = html;
  var uls = $("<ul>").html(data)[0].getElementsByTagName('ul');
  var length = uls.length;
  var elements;
  var object = {
    id: 'meh',
    name: '',
    lastN: '',
    identyfier: '',
  };
  var arrayb = new Array();
  for (var i = 2; i < length; ++i) {
    elements = $("<ul>").html(data)[0].getElementsByTagName('ul')[i].getElementsByTagName('li');

    var elemLength = elements.length;
    object.id = elements[0].firstChild.nodeValue;
    object.name = elements[1].firstChild.nodeValue;
    object.lastN = elements[2].firstChild.nodeValue;
    object.identyfier = elements[3].firstChild.nodeValue;
    arrayb.push(object);
    console.log(object);
  }
  console.log(arrayb);
}, "text");

输出:

Object { id: "1", name: "Peter", lastN: "La'habo", identyfier: "ABsdf23" }

Object { id: "2", name: "Liam", lastN: "Peters", identyfier: "dfghfgh54" }

Object { id: "3", name: "Super", lastN: "man", identyfier: "gff$$#'\\" }

Object { id: "4", name: "Jimmy\t", lastN: "Bobb's", identyfier: "fghj234\"234df" }

Object { id: "5", name: "Richárd", lastN: "Moore", identyfier: "dfghfg" }

Object { id: "6", name: "Jack", lastN: "Hurlock", identyfier: " fffffff" }

Object { id: "7", name: "Steve", lastN: "Stanley", identyfier: "fghgggg433" }

array

0: Object { id: "7", name: "Steve", lastN: "Stanley", … }

1: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
2: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
3: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
4: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
5: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
6: Object { id: "7", name: "Steve", lastN: "Stanley", … }
​
length: 7
提问于
用户回答回答于

假设其他内容都是正确的,下面是一种稍微简单一些的声明每个对象的方法:

$.get("Readfrom.php", null, function(html) {
  var arrayb = $("<ul>")
    .html(html)
    .find('ul')
    .toArray()
    .slice(2) // starting at index 2
    .map(function (ul) {
      var elements = $(ul).children('li');

      return {
        id: elements[0].firstChild.nodeValue,
        name: elements[1].firstChild.nodeValue,
        lastN: elements[2].firstChild.nodeValue,
        identyfier: elements[3].firstChild.nodeValue
      };
    });

  console.log(arrayb);
}, "text");
用户回答回答于

可以这样试试:

    $.get("Readfrom.php",null,function(html){
//console.log(html);
var data=html;
var uls=$("<ul>").html(data)[0].getElementsByTagName('ul');
var length= uls.length;
var elements;
var arrayb= new Array();
for (var i=2;i<length;++i)
{

let object={
id:'meh',
name:'',
lastN:'',
identyfier:'',
};
elements=$("<ul>").html(data)[0].getElementsByTagName('ul')[i].getElementsByTagName('li');

var elemLength=elements.length;
object.id=elements[0].firstChild.nodeValue;
object.name=elements[1].firstChild.nodeValue;
object.lastN=elements[2].firstChild.nodeValue;
object.identyfier=elements[3].firstChild.nodeValue;
arrayb.push(object);
console.log(object);


}
console.log(arrayb);
},"text");

扫码关注云+社区

领取腾讯云代金券