Javascript 检查对象文字数组中的重复值,并从中创建新对象
我目前拥有任何对象文本数组,它们有可能以重复的值进入客户端(请参见Javascript 检查对象文字数组中的重复值,并从中创建新对象,javascript,arrays,object-literal,Javascript,Arrays,Object Literal,我目前拥有任何对象文本数组,它们有可能以重复的值进入客户端(请参见Date键) 如何检查上一个键的值以将其合并到一个新对象中,例如: [ { "Date": "2/26/2018", "Title": "Story 1" }, { "Date": "2/27/2018", "Stories": { [ { "Titl
Date
键)
如何检查上一个键的值以将其合并到一个新对象中,例如:
[
{
"Date": "2/26/2018",
"Title": "Story 1"
},
{
"Date": "2/27/2018",
"Stories": {
[
{
"Title": "Story 2"
},
{
"Title": "Story 3"
}
]
}
},
{
"Date": "2/28/2018",
"Title": "Story 4"
}
]
关键是,不能使用下划线.js这样的框架
想法?您可以使用功能
reduce
var数组=[{“日期”:“2018年2月26日”,“标题”:“故事1”},{“日期”:“2018年2月27日”,“标题”:“故事2”},{“日期”:“2018年2月27日”,“标题”:“故事3”},{“日期”:“2018年2月28日”,“标题”:“故事4”});
var result=Object.values(array.reduce)(a,c)=>{
如果(a[c.日期]){
a[c.Date].Stories.push({Title:c.Title});
}否则{
a[c.Date]={“Date”:c.Date,“Stories”:[{Title:c.Title}]};
}
返回a;
}, {}));
控制台日志(结果)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以使用函数reduce
var数组=[{“日期”:“2018年2月26日”,“标题”:“故事1”},{“日期”:“2018年2月27日”,“标题”:“故事2”},{“日期”:“2018年2月27日”,“标题”:“故事3”},{“日期”:“2018年2月28日”,“标题”:“故事4”});
var result=Object.values(array.reduce)(a,c)=>{
如果(a[c.日期]){
a[c.Date].Stories.push({Title:c.Title});
}否则{
a[c.Date]={“Date”:c.Date,“Stories”:[{Title:c.Title}]};
}
返回a;
}, {}));
控制台日志(结果)代码>
.as console wrapper{max height:100%!important;top:0;}
我认为您需要进一步分解这个问题
这与SQL中的groupby
语句非常相似-本质上,在这种情况下,您是按Date
进行分组的
我想这样做:
function parseStories(stories) {
// we're going to record previous stories in an object using their date, as that is the value we wish to group by
var previousStories = {};
stories.forEach(function (storyObj) {
// this is how we should insert the object - it fits the "shape" of object retrieval where there is more than 1 result
var insertObj = { Title: storyObj.Title };
if (previousStories[storyObj.Date]) {
// date already exists
previousStories[storyObj.Date].push(insertObj);
} else {
// new date
previousStories[storyObj.Date] = [insertObj];
}
});
// we generate the return result
var returnResult = [];
// NOTE - object property order is not guaranteed; however, you can mitigate this by using an array to sort the keys if you so wish
for (var key in previousStories) {
if (previousStories[key].length > 1) {
// if we have more than one story on a day, we can just add this to the Stories property
returnResult.push({
Date: key,
Stories: previousStories[key]
});
} else if (previousStories[key].length === 1) {
// if we only have one story, the structure of the return object changes slightly
returnResult.push({
Date: key,
Title: previousStories[key][0].Title
});
}
}
return returnResult;
}
你的分组有相当多的逻辑性;当只有一个元素时,您想要生成的对象的结构会发生微妙的变化,从具有故事
数组到具有包含该故事标题的单个标题
属性。也许这个设计可以改进一下?通常,在单个数据结构上编写代码更容易,而不是根据其内容的多个而改变的数据结构。我认为您需要进一步分解这个问题
这与SQL中的groupby
语句非常相似-本质上,在这种情况下,您是按Date
进行分组的
我想这样做:
function parseStories(stories) {
// we're going to record previous stories in an object using their date, as that is the value we wish to group by
var previousStories = {};
stories.forEach(function (storyObj) {
// this is how we should insert the object - it fits the "shape" of object retrieval where there is more than 1 result
var insertObj = { Title: storyObj.Title };
if (previousStories[storyObj.Date]) {
// date already exists
previousStories[storyObj.Date].push(insertObj);
} else {
// new date
previousStories[storyObj.Date] = [insertObj];
}
});
// we generate the return result
var returnResult = [];
// NOTE - object property order is not guaranteed; however, you can mitigate this by using an array to sort the keys if you so wish
for (var key in previousStories) {
if (previousStories[key].length > 1) {
// if we have more than one story on a day, we can just add this to the Stories property
returnResult.push({
Date: key,
Stories: previousStories[key]
});
} else if (previousStories[key].length === 1) {
// if we only have one story, the structure of the return object changes slightly
returnResult.push({
Date: key,
Title: previousStories[key][0].Title
});
}
}
return returnResult;
}
你的分组有相当多的逻辑性;当只有一个元素时,您想要生成的对象的结构会发生微妙的变化,从具有故事
数组到具有包含该故事标题的单个标题
属性。也许这个设计可以改进一下?通常,在单个数据结构上进行编码更容易,而不是根据其内容的多个而改变的数据结构。一旦您只想创建一个故事
对象,而它有标题
要分组,我想这应该可以:
arr.reduce((acc, ele) => {
if(acc.length == 0) return acc.concat(ele);
var previous = acc[acc.length - 1];
if(ele.Date == previous.Date) {
if(!previous.Stories) previous.Stories = [{ title: previous.Title }];
previous.Stories.push({ title: ele.Title })
delete previous.Title;
return acc;
}
return acc.concat(ele);
}, [])
一旦你只想创建一个故事
对象,当它有标题
要分组时,我认为这应该是可行的:
arr.reduce((acc, ele) => {
if(acc.length == 0) return acc.concat(ele);
var previous = acc[acc.length - 1];
if(ele.Date == previous.Date) {
if(!previous.Stories) previous.Stories = [{ title: previous.Title }];
previous.Stories.push({ title: ele.Title })
delete previous.Title;
return acc;
}
return acc.concat(ele);
}, [])
如果原始数组的第一个值没有值,如何使用此选项防止“TypeError:减少没有初始值的空数组”?如果原始数组的第一个值没有值,如何使用此选项防止“TypeError:减少没有初始值的空数组”?