【发布时间】:2021-04-26 16:55:24
【问题描述】:
我正在尝试从数组中追加对象。我尝试遍历数组,但它只将数组的一个元素附加到对象。 小提琴:https://jsfiddle.net/maherafrasiab/9ehkfvoc/3/
已编辑:
var shers = {};
// the .pbgmain will return the following array.
var all = [{sher: "some text"},{sher: "some text"},{sher: "some text"}];
//basically i want to convert the above array into object.
var data = $('.pbgmain').each(function(i) {
datas = $(this).text().trim();
all.push({sher: datas})
for(var i = 0; i < 10; i++) {
shers = all[i];
}
});
【问题讨论】:
-
你一直在覆盖
shers。你打算shers是什么?它的属性是什么? -
@trincot 这些是乌尔都语诗歌的对联。它的属性将是每一个对联。
-
你为什么要精确地迭代
10次?如果少于 10 件怎么办? -
将值“附加”到
object是没有意义的。object仅包含键控属性(即命名属性)。我认为您误解了 JavaScript 的object和array类型的工作原理。 -
@Dai 该数组包含 10 个元素。这就是我要迭代的内容
标签: javascript arrays json loops object