合并两个Javascript对象数组时出现问题
我有一个对象数组。我只想再添加一个带有索引的对象。我不知道是怎么做的 这就是我尝试过的:合并两个Javascript对象数组时出现问题,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个对象数组。我只想再添加一个带有索引的对象。我不知道是怎么做的 这就是我尝试过的: //RAW DATA: this.fetchedData = [{ "startMinute": 0, //Remove "annotation": "All Day", "twelveHourFormat": "12am-11:59pm", //Remove "timeRange": "00:00-23:59", "endMinute": 1439 //Remove }, { "s
//RAW DATA:
this.fetchedData = [{
"startMinute": 0, //Remove
"annotation": "All Day",
"twelveHourFormat": "12am-11:59pm", //Remove
"timeRange": "00:00-23:59",
"endMinute": 1439 //Remove
}, {
"startMinute": 300, //Remove
"annotation": "Morning",
"twelveHourFormat": "5am-8:59am", //Remove
"timeRange": "05:00-08:59",
"endMinute": 539 //Remove
}]
var newTimeRanges = [];
var temp = []; //Final array(assuming i need one)
newTimeRanges = _.each(this.fetchedData, function(time) {
delete time.startMinute;
delete time.twelveHourFormat;
delete time.endMinute;
});
//DATA AFTER REMOVAL
newTimeRanges = {
0: {
annotation: "All Day",
timeRange: "00:00-23:59"
},
1: {
annotation: "Evening",
timeRange: "16:00-18:00"
}
}
//DATA TO BE MERGED
var dirtyData = [{
"timeRange": "3am-6am",
"annotation": earlymorning
}];
//Essentially (timeRange+DirtyData)
//Expected Output to be sent for backend
{
"times": [{
"annotation": "All Day",
"timeRange": "00:00-23:59"
}, {
"annotation": "Morning",
"timeRange": "05:00-08:59"
},
//Add my new Data here
]
}
现在,我想向该数组添加或删除另一个对象
temp.push(newTimeRanges);
temp.push(dirtyData);
这不会合并记录。而是创建两个对象—一个对象和另一个数组
如何合并。带有DirtyData的newTimeRanges。(就像我想要的3:“对象”)
有什么有效的方法可以做到这一点吗?
谢谢您需要将
newTimeRanges
的每个属性分别推送到temp
数组中。然后可以连接dirtyData
$.each(newTimeRanges, function(key, obj) {
temp.push(obj);
}
temp = temp.concat(dirtyData);
或者您可以制作newTimeRanges
数组而不是对象,然后您可以编写:
newTimeRanges = [
{
annotation:"All Day",
timeRange:"00:00-23:59"},
{
annotation:"Evening",
timeRange:"16:00-18:00"}
];
temp = newTimeRanges.concat(dirtyData);
如果要将2个数组合并到第三个数组中,必须使用concat,如下所示:
var temp = newTimeRanges.concat(dirtyData);
见:
更改代码以使其工作:
//RAW DATA:
var fetchedData = [{
"startMinute": 0, //Remove
"annotation": "All Day",
"twelveHourFormat": "12am-11:59pm", //Remove
"timeRange": "00:00-23:59",
"endMinute": 1439 //Remove
}, {
"startMinute": 300, //Remove
"annotation": "Morning",
"twelveHourFormat": "5am-8:59am", //Remove
"timeRange": "05:00-08:59",
"endMinute": 539 //Remove
}];
fetchedData.forEach(function(time) {
delete time.startMinute;
delete time.twelveHourFormat;
delete time.endMinute;
});
console.log(fetchedData);
//DATA TO BE MERGED
var dirtyData = [{
"timeRange": "3am-6am",
"annotation": "earlymorning"
}];
var temp = fetchedData.concat(dirtyData);
console.log(temp);
为什么不
temp=newTimeRanges;温度推送(dirtyData[0])//或者循环使用它
请添加原始数据和所需结果,如对象文字。newTimeRanges
以数组形式开始,但当您记录它时,它变成了一个对象。您是如何填充它的?如果要立即用对象替换它,为什么要用空数组初始化newTimeRanges
?@Barmar当我使用下划线循环遍历循环并删除一些转换为对象的内容时。我不得不删除一堆东西并将其存储回去。现在我想附加到新创建的对象。@Barmar问题是关于数组。。。或者应该是,应该是,但请阅读问题。他用一个碰巧有数字属性名的对象替换数组。但这不是一个数组。是的,他在我们说话的时候改变了它,所以我坚持标题,因为他似乎对代码一无所知。我的意思是,他似乎可以用数组实现他想要的。谢谢大家的帮助。我成功了!对不起,如果我把你们搞糊涂了!