how to make array of objects from array based on some condtion in javascript
i have an array like let x = [1,5,2,6,7,9]
but i want to make this as array of objects like below code map or JSON
let y = [
{ st:1,ed:2},
{st:5,ed:7},
{st:9,ed:9}
]
based on the continuity of the digits, any help or suggestion required
javascript
add a comment |
i have an array like let x = [1,5,2,6,7,9]
but i want to make this as array of objects like below code map or JSON
let y = [
{ st:1,ed:2},
{st:5,ed:7},
{st:9,ed:9}
]
based on the continuity of the digits, any help or suggestion required
javascript
2
what happens to6
? why the random order?
– Nina Scholz
Nov 24 '18 at 12:02
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
1
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12
add a comment |
i have an array like let x = [1,5,2,6,7,9]
but i want to make this as array of objects like below code map or JSON
let y = [
{ st:1,ed:2},
{st:5,ed:7},
{st:9,ed:9}
]
based on the continuity of the digits, any help or suggestion required
javascript
i have an array like let x = [1,5,2,6,7,9]
but i want to make this as array of objects like below code map or JSON
let y = [
{ st:1,ed:2},
{st:5,ed:7},
{st:9,ed:9}
]
based on the continuity of the digits, any help or suggestion required
javascript
javascript
asked Nov 24 '18 at 12:00
Hemanth SPHemanth SP
447
447
2
what happens to6
? why the random order?
– Nina Scholz
Nov 24 '18 at 12:02
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
1
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12
add a comment |
2
what happens to6
? why the random order?
– Nina Scholz
Nov 24 '18 at 12:02
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
1
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12
2
2
what happens to
6
? why the random order?– Nina Scholz
Nov 24 '18 at 12:02
what happens to
6
? why the random order?– Nina Scholz
Nov 24 '18 at 12:02
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
1
1
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12
add a comment |
2 Answers
2
active
oldest
votes
You could sort the array and reduce the array by inserting a new object or updat the end vlaue, depending on the first item or if the last value is not in short range.
In parts:
.sort((a, b) => a - b)
sorts the array by taking the delta of twor elements and returns the needed value for using
Array#some
. The array fter sorting looks like
[1, 2, 5, 6, 7, 9]
The more complex part is to use
Array#reduce
which returns the final array of objects.
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
At the beginning, where
index
is zero, the first object is pushed to theaccumulator
.
Then it takes the next value form the array and while
!1
is not true, the second part of the check
accumulator[accumulator.length - 1].end + 1 < value
is evaluated and returns
false
, so the else part updates theend
property.
Finally the accumulator is returned and contains the wanted result.
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
add a comment |
You can use Array.sort
, Array.filter
and Array.flatMap
as well like below
sort
will sort the array in increasing order and will result in -[1,2,5,6,7,9]
filter
will then filter those results whose value is continuous when checked with left and right values and so it will result in -[1,2,5,7,9]
as only6
is value which has just difference of value 1 when compared with left and right valuesflatMap
will then loop through the above result and prepare your desired output
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457927%2fhow-to-make-array-of-objects-from-array-based-on-some-condtion-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could sort the array and reduce the array by inserting a new object or updat the end vlaue, depending on the first item or if the last value is not in short range.
In parts:
.sort((a, b) => a - b)
sorts the array by taking the delta of twor elements and returns the needed value for using
Array#some
. The array fter sorting looks like
[1, 2, 5, 6, 7, 9]
The more complex part is to use
Array#reduce
which returns the final array of objects.
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
At the beginning, where
index
is zero, the first object is pushed to theaccumulator
.
Then it takes the next value form the array and while
!1
is not true, the second part of the check
accumulator[accumulator.length - 1].end + 1 < value
is evaluated and returns
false
, so the else part updates theend
property.
Finally the accumulator is returned and contains the wanted result.
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
add a comment |
You could sort the array and reduce the array by inserting a new object or updat the end vlaue, depending on the first item or if the last value is not in short range.
In parts:
.sort((a, b) => a - b)
sorts the array by taking the delta of twor elements and returns the needed value for using
Array#some
. The array fter sorting looks like
[1, 2, 5, 6, 7, 9]
The more complex part is to use
Array#reduce
which returns the final array of objects.
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
At the beginning, where
index
is zero, the first object is pushed to theaccumulator
.
Then it takes the next value form the array and while
!1
is not true, the second part of the check
accumulator[accumulator.length - 1].end + 1 < value
is evaluated and returns
false
, so the else part updates theend
property.
Finally the accumulator is returned and contains the wanted result.
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
add a comment |
You could sort the array and reduce the array by inserting a new object or updat the end vlaue, depending on the first item or if the last value is not in short range.
In parts:
.sort((a, b) => a - b)
sorts the array by taking the delta of twor elements and returns the needed value for using
Array#some
. The array fter sorting looks like
[1, 2, 5, 6, 7, 9]
The more complex part is to use
Array#reduce
which returns the final array of objects.
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
At the beginning, where
index
is zero, the first object is pushed to theaccumulator
.
Then it takes the next value form the array and while
!1
is not true, the second part of the check
accumulator[accumulator.length - 1].end + 1 < value
is evaluated and returns
false
, so the else part updates theend
property.
Finally the accumulator is returned and contains the wanted result.
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could sort the array and reduce the array by inserting a new object or updat the end vlaue, depending on the first item or if the last value is not in short range.
In parts:
.sort((a, b) => a - b)
sorts the array by taking the delta of twor elements and returns the needed value for using
Array#some
. The array fter sorting looks like
[1, 2, 5, 6, 7, 9]
The more complex part is to use
Array#reduce
which returns the final array of objects.
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
At the beginning, where
index
is zero, the first object is pushed to theaccumulator
.
Then it takes the next value form the array and while
!1
is not true, the second part of the check
accumulator[accumulator.length - 1].end + 1 < value
is evaluated and returns
false
, so the else part updates theend
property.
Finally the accumulator is returned and contains the wanted result.
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var array = [1, 5, 2, 6, 7, 9],
result = array
.sort((a, b) => a - b)
.reduce((accumulator, value, index) => {
if (!index || accumulator[accumulator.length - 1].end + 1 < value) {
accumulator.push({ start: value, end: value });
} else {
accumulator[accumulator.length - 1].end = value;
}
return accumulator;
}, );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
edited Nov 24 '18 at 12:41
answered Nov 24 '18 at 12:10
Nina ScholzNina Scholz
185k1596168
185k1596168
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
add a comment |
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
oh my undertaker it complex answer can you add more info at least i will try to understad
– Hemanth SP
Nov 24 '18 at 12:18
add a comment |
You can use Array.sort
, Array.filter
and Array.flatMap
as well like below
sort
will sort the array in increasing order and will result in -[1,2,5,6,7,9]
filter
will then filter those results whose value is continuous when checked with left and right values and so it will result in -[1,2,5,7,9]
as only6
is value which has just difference of value 1 when compared with left and right valuesflatMap
will then loop through the above result and prepare your desired output
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
add a comment |
You can use Array.sort
, Array.filter
and Array.flatMap
as well like below
sort
will sort the array in increasing order and will result in -[1,2,5,6,7,9]
filter
will then filter those results whose value is continuous when checked with left and right values and so it will result in -[1,2,5,7,9]
as only6
is value which has just difference of value 1 when compared with left and right valuesflatMap
will then loop through the above result and prepare your desired output
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
add a comment |
You can use Array.sort
, Array.filter
and Array.flatMap
as well like below
sort
will sort the array in increasing order and will result in -[1,2,5,6,7,9]
filter
will then filter those results whose value is continuous when checked with left and right values and so it will result in -[1,2,5,7,9]
as only6
is value which has just difference of value 1 when compared with left and right valuesflatMap
will then loop through the above result and prepare your desired output
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
You can use Array.sort
, Array.filter
and Array.flatMap
as well like below
sort
will sort the array in increasing order and will result in -[1,2,5,6,7,9]
filter
will then filter those results whose value is continuous when checked with left and right values and so it will result in -[1,2,5,7,9]
as only6
is value which has just difference of value 1 when compared with left and right valuesflatMap
will then loop through the above result and prepare your desired output
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
let x = [1,5,2,6,7,9]
let res = x.sort()
.filter((d, i, t) => !(d == t[i-1] + 1 && d == t[i+1] - 1))
.flatMap((d, i, t) => i%2 == 0 ? [{ st: d, en: t[i+1] || d }] : )
console.log(res)
edited Nov 24 '18 at 13:17
answered Nov 24 '18 at 12:51
Nitish NarangNitish Narang
2,9401815
2,9401815
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457927%2fhow-to-make-array-of-objects-from-array-based-on-some-condtion-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
what happens to
6
? why the random order?– Nina Scholz
Nov 24 '18 at 12:02
@Nina Scholz no need of 6 it just {st:5,ed:7} interview question, needed only start and end
– Hemanth SP
Nov 24 '18 at 12:03
This makes absolutely no sense to me ... unless I'm missing some piece of the puzzle.
– techouse
Nov 24 '18 at 12:03
1
what does it mean based on the continuity of the digits?
– Dennis Vash
Nov 24 '18 at 12:12