我目前有任何对象文字数组,有可能使用重复值进入客户端(请参阅Date
键)。
[
{
"Date": "2/26/2018",
"Title": "Story 1"
},
{
"Date": "2/27/2018",
"Title": "Story 2"
},
{
"Date": "2/27/2018",
"Title": "Story 3"
},
{
"Date": "2/28/2018",
"Title": "Story 4"
}
]
如何检查上一个键的值以将其合并到一个新对象中,例如:
[
{
"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"
}
]
这是踢球者 - 没有像Underscore.js这样的框架可以使用。
思考?
您可以使用reduce
功能
var array = [ { "Date": "2/26/2018", "Title": "Story 1" }, { "Date": "2/27/2018", "Title": "Story 2" }, { "Date": "2/27/2018", "Title": "Story 3" }, { "Date": "2/28/2018", "Title": "Story 4" }];
var result = Object.values(array.reduce((a, c) => {
if (a[c.Date]) {
a[c.Date].Stories.push({Title: c.Title});
} else {
a[c.Date] = { "Date": c.Date, "Stories": [{Title: c.Title}] };
}
return a;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我认为你需要更多地解决这个问题。
这与SQL中的GROUP BY
语句非常相似 - 基本上,在这种情况下,您将由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;
}
你的分组涉及相当多的逻辑;在只有一个元素的地方,你想要产生的对象的结构会巧妙地改变,从拥有Stories
数组到具有包含那个故事的标题的单一Title
属性。也许这个设计可以改进吗?通常更容易对单个数据结构进行编码,而不是基于其多个内容而改变的数据结构。
一旦你想要创建一个Stories
对象,当它有titles
被分组时,我认为这应该工作:
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);
}, [])