I am trying to get two objects from array using the splice()
method. But it is not working as expected. Please guide me where i am wrong or help me to fix this. if anybody can modify this code to a simpler version would be appreciated.
Basically, I am trying to join two objects title
and content
. So please help me to fix this.
const output = [{
"title": "Lorem Ipsum Test Title 1"
},
{
"content": "The short answer to this is yes."
},
{
"title": "Lorem Ipsum Test Title 2"
},
{
"content": "The short answer to this is yes."
},
{
"title": "Lorem Ipsum Test Title 3"
},
{
"content": "The short answer to this is yes."
},
{
"title": "Lorem Ipsum Test Title 4"
},
{
"content": "The short answer to this is yes."
}
];;
let start = 0;
let end = 2;
const finalOutput = [];
for (j = 0; j <= output.length; j ) {
const data = output.splice(start, end);
if (typeof data[0] != 'undefined') {
finalOutput.push({
title: data[0].title,
content: data[1].content
});
}
start = start 2;
end = end 2;
}
console.log(finalOutput)
The final output from the above code is below. It skips two objects.
CodePudding user response:
You have an issue in that output.length
changes during your iteration, and you are splicing from increasing indexes, although because you have spliced the array has shortened. If you want to keep using splice
, change your code as follows:
const output = [{
"title": "Lorem Ipsum Test Title"
},
{
"content": "The short answer to this is yes."
},
{
"title": "Lorem Ipsum Test Title #2"
},
{
"content": "The short answer to this is also yes."
},
{
"title": "Lorem Ipsum Test Title #3"
},
{
"content": "The short answer to this is not yes."
}
];
const finalOutput = [];
const len = output.length;
for (j = 0; j < len; j =2) {
const data = output.splice(0, 2);
finalOutput.push({
title: data[0].title,
content: data[1].content
});
}
console.log(finalOutput)
Alternatively, use slice
and the code might be more logical:
const output = [{
"title": "Lorem Ipsum Test Title"
},
{
"content": "The short answer to this is yes."
},
{
"title": "Lorem Ipsum Test Title #2"
},
{
"content": "The short answer to this is also yes."
},
{
"title": "Lorem Ipsum Test Title #3"
},
{
"content": "The short answer to this is not yes."
}
];
const finalOutput = [];
for (j = 0; j < output.length; j =2) {
const data = output.slice(j, j 2);
finalOutput.push({
title: data[0].title,
content: data[1].content
});
}
console.log(finalOutput)
Note in both cases start
and end
are redundant and you don't need the check on data[0]
as it is guaranteed to be valid.
CodePudding user response:
Or you can think about a method with more functional
const output = [{
"title": "Title 1"
},
{
"content": "Content 1"
},
{
"title": "Title 2"
},
{
"content": "Content 2"
},
{
"title": "Title 3"
},
{
"content": "Content 3"
}];
const reducer = (pre, cur) => {
if (cur.content) {
pre[pre.length - 1].content = cur.content;
return pre;
}
return [...pre, {
title: cur.title
}]
}
const join = output.reduce(reducer, [])
console.log(join) // [{"title":"Title 1","content":"Content 1"},{"title":"Title 2","content":"Content 2"},{"title":"Title 3","content":"Content 3"}]
CodePudding user response:
I think Ghost has the best answer - neat and simple.
I had something along those lines but not as neat:
output.map((value, index, array) => {
if (index == 0 || index % 2 == 0){
const contentIndex = index 1;
return { "title": value.title, "content": array[contentIndex].content }
}
}).filter(o => o !== undefined);