在 javascript 中合并动态对象数组
Merge dynamic object arrays in javascript
我正在尝试使用键动态构建一个对象:[值数组],但是使用不同的方法我总是以值数组中的单个项目结束(在响应中有多个值)。
伪代码:
var myFunction = function () {
var myObject = {};
$.ajax('http://the.url.com', {
type: "GET",
success: function (res) {
$(res).each(function (i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
myObject[name] = new Array();
myObject[name].push(id);
});
},
error: function (xhr) {}
}
}
当前输出:
{
key1: ["value1c"]
key2: ["value2a"]
key3: ["value3b"]
}
期望的输出:
{
key1: ["value1a", "value1b","value1c"]
key2: ["value2a"]
key3: ["value3a", "value3b"]
}
我认为您需要在创建新的之前检查 myObject[name]
是否已经存在。因为如果每次都创建一个新的,它会被覆盖
var myFunction = function () {
var myObject = {};
$.ajax('http://the.url.com', {
type: "GET",
success: function (res) {
$(res).each(function (i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
if (!myObject[name]) {
myObject[name] = new Array();
}
myObject[name].push(id);
});
},
error: function (xhr) {}
}
}
您正在为每个键用一个新数组覆盖现有数组,然后使用以下行推送最新的数组:
myObject[name] = new Array();
尝试添加检查以避免覆盖:
myObject[name] = myObject[name] || new Array();
输出是 key1: ["value1c"]
因为对象中的 key
是唯一的,所以它创建键并仅存储最新值。您可以使用 hasOwnProperty 并检查 myObject
是否具有该名称的任何密钥。如果是,则推送该值,否则创建一个键值对并向其添加 id
$(res).each(function(i, v) {
var name = v.name;
var id = v.id;
if (myObject.hasOwnProperty(name)) {
myObject[name].push(id);
} else {
myObject[name] = [id];
}
});
您每次使用此行创建另一个数组:
myObject[name] = new Array();
所以你每次都删除旧的推送值。
如果数组不存在,则使用条件初始化数组:
!Array.isArray(myObject[name]) && (myObject[name] = new Array());
例如
$(res).each(function(i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
!Array.isArray(myObject[name]) && (myObject[name] = new Array());
myObject[name].push(id);
});
我正在尝试使用键动态构建一个对象:[值数组],但是使用不同的方法我总是以值数组中的单个项目结束(在响应中有多个值)。
伪代码:
var myFunction = function () {
var myObject = {};
$.ajax('http://the.url.com', {
type: "GET",
success: function (res) {
$(res).each(function (i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
myObject[name] = new Array();
myObject[name].push(id);
});
},
error: function (xhr) {}
}
}
当前输出:
{
key1: ["value1c"]
key2: ["value2a"]
key3: ["value3b"]
}
期望的输出:
{
key1: ["value1a", "value1b","value1c"]
key2: ["value2a"]
key3: ["value3a", "value3b"]
}
我认为您需要在创建新的之前检查 myObject[name]
是否已经存在。因为如果每次都创建一个新的,它会被覆盖
var myFunction = function () {
var myObject = {};
$.ajax('http://the.url.com', {
type: "GET",
success: function (res) {
$(res).each(function (i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
if (!myObject[name]) {
myObject[name] = new Array();
}
myObject[name].push(id);
});
},
error: function (xhr) {}
}
}
您正在为每个键用一个新数组覆盖现有数组,然后使用以下行推送最新的数组:
myObject[name] = new Array();
尝试添加检查以避免覆盖:
myObject[name] = myObject[name] || new Array();
输出是 key1: ["value1c"]
因为对象中的 key
是唯一的,所以它创建键并仅存储最新值。您可以使用 hasOwnProperty 并检查 myObject
是否具有该名称的任何密钥。如果是,则推送该值,否则创建一个键值对并向其添加 id
$(res).each(function(i, v) {
var name = v.name;
var id = v.id;
if (myObject.hasOwnProperty(name)) {
myObject[name].push(id);
} else {
myObject[name] = [id];
}
});
您每次使用此行创建另一个数组:
myObject[name] = new Array();
所以你每次都删除旧的推送值。
如果数组不存在,则使用条件初始化数组:
!Array.isArray(myObject[name]) && (myObject[name] = new Array());
例如
$(res).each(function(i, v) {
var name = v.name;
var id = v.id;
// create object with building block and tech id to associate techs to BBs
!Array.isArray(myObject[name]) && (myObject[name] = new Array());
myObject[name].push(id);
});