gpt4 book ai didi

javascript - 如何在 $.each(?

转载 作者:行者123 更新时间:2023-11-30 07:55:49 24 4
gpt4 key购买 nike

我当前的代码将键压入普通数组,但我想将键和值都压入对象数组。如果专家告诉我如何做到这一点,我将不胜感激。谢谢

创建对象数组的 Javascript:

var xml ='<?xml version="1.0" encoding="UTF-8"?> <dict><key>ItemLists</key> <array><dict><key>id</key> <string>1</string> <key>name</key> <string>fruits</string> <key>category</key> <string>US Fruits</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/1.jpg</string> <key>country</key> <string>US</string> </dict> <dict><key>id</key> <string>2</string> <key>name</key> <string>Vegetable</string> <key>category</key> <string>Eu Vegetable</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/2.jpg</string> <key>country</key> <string>EU</string> </dict> </array> </dict>';

xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);

var MyArray=[];
$(xml).find("array key").each(function(){
var key = $(this).text();
var value = $(this).next().text();
console.log(key + "=" + value);
//here i want to create array of objects instead of normal array
//and push both key and value to array of object
MyArray.push(key);
});

我想要构造的对象数组示例:

var myArray = [{
id: 1,
name: 'fruits',
category: 'US Fruits',
categoryIcon: 'http://www.somsite.com/categories/1.jpg',
country: 'US'
}, {
id: 2,
name: 'Vegetable',
category: 'Eu Vegetable',
categoryIcon: 'http://www.somsite.com/categories/2.jpg',
country: 'EU'
}, ];

编辑:我试图像这样打印对象数组,但我对所有对象都未定义值(value)观!:

$.each(MyArray,function(j, object){

var div = "<tr id=\""+j+"\">\n" +
"<td>"+j+"</td>\n" +
"<td><img src=\""+ object.categoryIcon +"\" height=\"42\" width=\"42\"></td>\n" +
"<td>\n" +
"<a href=\"javascript:doit('id=" + object.id + "&name=" + object.name + "&category=" + object.category + "&categoryIcon=" + object.categoryIcon + "','"+ object.country +"')\" onclick=\"selectLink(this);\">" + object.name + "</a><br> \n" +
"<br></td></tr>\n\n";
$("#myDiv").append(div);

});

最佳答案

如果你只是替换

MyArray.push(key);

var o = {};
o[key] = value;

MyArray.push(o);

它会那样做,但您似乎想遍历每个 dict , 然后得到每个 key 的值在 parent 内部 dict

var xml = '<?xml version="1.0" encoding="UTF-8"?> <dict><key>ItemLists</key> <array><dict><key>id</key> <string>1</string> <key>name</key> <string>fruits</string> <key>category</key> <string>US Fruits</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/1.jpg</string> <key>country</key> <string>US</string> </dict> <dict><key>id</key> <string>2</string> <key>name</key> <string>Vegetable</string> <key>category</key> <string>Eu Vegetable</string> <key>categoryIcon</key> <string>http://www.somsite.com/categories/2.jpg</string> <key>country</key> <string>EU</string> </dict> </array> </dict>';

var xmlDoc = $.parseXML(xml),
$xml = $(xmlDoc);

var MyArray = [];
$(xml).find("array dict").each(function(_,elem) {
var o = {};
$('key', elem).each(function() {
var key = $(this).text();
var value = $(this).next().text();
o[key] = value;
});
MyArray.push(o);
});
console.log(MyArray)
.as-console-wrapper {top:0; min-height:100%!important}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

或者,您可以使用 map()reduce()

var MyArray = $(xml).find("array dict").map(function(_,elem) {
return $('key', elem).toArray().reduce(function(a,b) {
return a[$(b).text()] = $(b).next().text(), a;
}, {});
}).get();

关于javascript - 如何在 $.each(?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com