Looping over JavaScript object and adding string to end if not the last item
up vote
1
down vote
favorite
{
field_country: ["England", "Netherlands", "India", "Italy"],
field_continent: ["Europe"],
field_group: ["Building", "People", "Landscape"
}
I want to loop over each item and return the key and the array together with ending 'OR' for example:
field_country: "England" OR field_country: "Netherlands"
The last item should not end with 'OR' in the loop. I am not sure what the best process is for this using vanilla JS. So far my code is as follows:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i == 1) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
I would be very grateful for any assistance.
Thanks in advance.
javascript arrays string foreach
add a comment |
up vote
1
down vote
favorite
{
field_country: ["England", "Netherlands", "India", "Italy"],
field_continent: ["Europe"],
field_group: ["Building", "People", "Landscape"
}
I want to loop over each item and return the key and the array together with ending 'OR' for example:
field_country: "England" OR field_country: "Netherlands"
The last item should not end with 'OR' in the loop. I am not sure what the best process is for this using vanilla JS. So far my code is as follows:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i == 1) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
I would be very grateful for any assistance.
Thanks in advance.
javascript arrays string foreach
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.
– Drew Reese
Nov 22 at 0:53
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
{
field_country: ["England", "Netherlands", "India", "Italy"],
field_continent: ["Europe"],
field_group: ["Building", "People", "Landscape"
}
I want to loop over each item and return the key and the array together with ending 'OR' for example:
field_country: "England" OR field_country: "Netherlands"
The last item should not end with 'OR' in the loop. I am not sure what the best process is for this using vanilla JS. So far my code is as follows:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i == 1) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
I would be very grateful for any assistance.
Thanks in advance.
javascript arrays string foreach
{
field_country: ["England", "Netherlands", "India", "Italy"],
field_continent: ["Europe"],
field_group: ["Building", "People", "Landscape"
}
I want to loop over each item and return the key and the array together with ending 'OR' for example:
field_country: "England" OR field_country: "Netherlands"
The last item should not end with 'OR' in the loop. I am not sure what the best process is for this using vanilla JS. So far my code is as follows:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i == 1) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
I would be very grateful for any assistance.
Thanks in advance.
javascript arrays string foreach
javascript arrays string foreach
asked Nov 21 at 23:57
Peter Ayello Wright
315
315
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.
– Drew Reese
Nov 22 at 0:53
add a comment |
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.
– Drew Reese
Nov 22 at 0:53
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the
" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.– Drew Reese
Nov 22 at 0:53
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the
" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.– Drew Reese
Nov 22 at 0:53
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Your conditional expression if (i == 1)
would only trigger on the second iteration of the loop since i
will only equal 1
one time.
Try something like:
if (i < (facetResults.length - 1)) {
// only add OR if this isn't the last element of the array
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
}
Here's your updated code:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i < (facetResults.length - 1)) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
add a comment |
up vote
2
down vote
You can do something like this with Object.entries
and Array.reduce
if you would like to get the final result in the form of an object:
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
It is somewhat unclear what is the final format you need to result in but that should help you to get the idea. If ES6 is not an option you can convert this to:
const result = Object.entries(data).reduce(function(r, [k, v]) {
r[k] = v.join(' OR ')
return r
}, {})
So there are is no arrow function
etc.
The idea is to get the arrays into the arrays of strings and use the Array.join to do the "replacement" for you via join(' OR ')
1
Instead ofv.toString().replace(/,/g, ' OR ')
you can use join:v.join(' OR ')
.
– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
add a comment |
up vote
1
down vote
Here's the idea. In your code you are appending " or " at the end of your strings starting at index 0. I suggest you append it at the the beginning starting at index 1.
var somewords = ["ORANGE", "GREEN", "BLUE", "WHITE" ];
var retval = somewords[0];
for(var i = 1; i< somewords.length; i++)
{
retval += " or " + somewords[i];
}
console.log(retval);
//result is: ORANGE or GREEN or BLUE or WHITE
1
Just setretval = some words[0];
, start at index 1, and do just your else branch.
– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your conditional expression if (i == 1)
would only trigger on the second iteration of the loop since i
will only equal 1
one time.
Try something like:
if (i < (facetResults.length - 1)) {
// only add OR if this isn't the last element of the array
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
}
Here's your updated code:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i < (facetResults.length - 1)) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
add a comment |
up vote
1
down vote
accepted
Your conditional expression if (i == 1)
would only trigger on the second iteration of the loop since i
will only equal 1
one time.
Try something like:
if (i < (facetResults.length - 1)) {
// only add OR if this isn't the last element of the array
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
}
Here's your updated code:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i < (facetResults.length - 1)) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your conditional expression if (i == 1)
would only trigger on the second iteration of the loop since i
will only equal 1
one time.
Try something like:
if (i < (facetResults.length - 1)) {
// only add OR if this isn't the last element of the array
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
}
Here's your updated code:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i < (facetResults.length - 1)) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
Your conditional expression if (i == 1)
would only trigger on the second iteration of the loop since i
will only equal 1
one time.
Try something like:
if (i < (facetResults.length - 1)) {
// only add OR if this isn't the last element of the array
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
}
Here's your updated code:
Object.keys(facets).forEach(function(facetKey) {
if (facets[facetKey].length > 1) {
facetResults = facets[facetKey];
for (var i = 0; i < facetResults.length; i ++) {
if (i < (facetResults.length - 1)) {
filter = "'" + facetKey + "'" + ":'" + facetResults[i] + " OR";
return filter;
} else {
filter = "'" + facetKey + "'" + ":'" + facetResults[i];
}
}
} else {
filter = "'" + facetKey + "'" + ": " + facets[facetKey] + "'";
return filter;
}
});
answered Nov 22 at 0:07
Nunchy
838410
838410
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
add a comment |
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
Thank you @Nunchy. That worked nicely
– Peter Ayello Wright
Nov 22 at 10:48
add a comment |
up vote
2
down vote
You can do something like this with Object.entries
and Array.reduce
if you would like to get the final result in the form of an object:
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
It is somewhat unclear what is the final format you need to result in but that should help you to get the idea. If ES6 is not an option you can convert this to:
const result = Object.entries(data).reduce(function(r, [k, v]) {
r[k] = v.join(' OR ')
return r
}, {})
So there are is no arrow function
etc.
The idea is to get the arrays into the arrays of strings and use the Array.join to do the "replacement" for you via join(' OR ')
1
Instead ofv.toString().replace(/,/g, ' OR ')
you can use join:v.join(' OR ')
.
– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
add a comment |
up vote
2
down vote
You can do something like this with Object.entries
and Array.reduce
if you would like to get the final result in the form of an object:
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
It is somewhat unclear what is the final format you need to result in but that should help you to get the idea. If ES6 is not an option you can convert this to:
const result = Object.entries(data).reduce(function(r, [k, v]) {
r[k] = v.join(' OR ')
return r
}, {})
So there are is no arrow function
etc.
The idea is to get the arrays into the arrays of strings and use the Array.join to do the "replacement" for you via join(' OR ')
1
Instead ofv.toString().replace(/,/g, ' OR ')
you can use join:v.join(' OR ')
.
– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
add a comment |
up vote
2
down vote
up vote
2
down vote
You can do something like this with Object.entries
and Array.reduce
if you would like to get the final result in the form of an object:
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
It is somewhat unclear what is the final format you need to result in but that should help you to get the idea. If ES6 is not an option you can convert this to:
const result = Object.entries(data).reduce(function(r, [k, v]) {
r[k] = v.join(' OR ')
return r
}, {})
So there are is no arrow function
etc.
The idea is to get the arrays into the arrays of strings and use the Array.join to do the "replacement" for you via join(' OR ')
You can do something like this with Object.entries
and Array.reduce
if you would like to get the final result in the form of an object:
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
It is somewhat unclear what is the final format you need to result in but that should help you to get the idea. If ES6 is not an option you can convert this to:
const result = Object.entries(data).reduce(function(r, [k, v]) {
r[k] = v.join(' OR ')
return r
}, {})
So there are is no arrow function
etc.
The idea is to get the arrays into the arrays of strings and use the Array.join to do the "replacement" for you via join(' OR ')
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
const data = { field_country: ["England", "Netherlands", "India", "Italy"], field_continent: ["Europe"], field_group: ["Building", "People", "Landscape"] }
const result = Object.entries(data).reduce((r, [k, v]) => {
r[k] = v.join(' OR ')
return r
}, {})
console.log(result)
edited Nov 22 at 0:36
answered Nov 22 at 0:07
Akrion
7,52611222
7,52611222
1
Instead ofv.toString().replace(/,/g, ' OR ')
you can use join:v.join(' OR ')
.
– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
add a comment |
1
Instead ofv.toString().replace(/,/g, ' OR ')
you can use join:v.join(' OR ')
.
– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
1
1
Instead of
v.toString().replace(/,/g, ' OR ')
you can use join: v.join(' OR ')
.– slider
Nov 22 at 0:29
Instead of
v.toString().replace(/,/g, ' OR ')
you can use join: v.join(' OR ')
.– slider
Nov 22 at 0:29
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
Sure. Good call. Thanks.
– Akrion
Nov 22 at 0:30
add a comment |
up vote
1
down vote
Here's the idea. In your code you are appending " or " at the end of your strings starting at index 0. I suggest you append it at the the beginning starting at index 1.
var somewords = ["ORANGE", "GREEN", "BLUE", "WHITE" ];
var retval = somewords[0];
for(var i = 1; i< somewords.length; i++)
{
retval += " or " + somewords[i];
}
console.log(retval);
//result is: ORANGE or GREEN or BLUE or WHITE
1
Just setretval = some words[0];
, start at index 1, and do just your else branch.
– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
add a comment |
up vote
1
down vote
Here's the idea. In your code you are appending " or " at the end of your strings starting at index 0. I suggest you append it at the the beginning starting at index 1.
var somewords = ["ORANGE", "GREEN", "BLUE", "WHITE" ];
var retval = somewords[0];
for(var i = 1; i< somewords.length; i++)
{
retval += " or " + somewords[i];
}
console.log(retval);
//result is: ORANGE or GREEN or BLUE or WHITE
1
Just setretval = some words[0];
, start at index 1, and do just your else branch.
– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
add a comment |
up vote
1
down vote
up vote
1
down vote
Here's the idea. In your code you are appending " or " at the end of your strings starting at index 0. I suggest you append it at the the beginning starting at index 1.
var somewords = ["ORANGE", "GREEN", "BLUE", "WHITE" ];
var retval = somewords[0];
for(var i = 1; i< somewords.length; i++)
{
retval += " or " + somewords[i];
}
console.log(retval);
//result is: ORANGE or GREEN or BLUE or WHITE
Here's the idea. In your code you are appending " or " at the end of your strings starting at index 0. I suggest you append it at the the beginning starting at index 1.
var somewords = ["ORANGE", "GREEN", "BLUE", "WHITE" ];
var retval = somewords[0];
for(var i = 1; i< somewords.length; i++)
{
retval += " or " + somewords[i];
}
console.log(retval);
//result is: ORANGE or GREEN or BLUE or WHITE
edited Nov 22 at 2:17
answered Nov 22 at 0:44
blaze_125
1,8771416
1,8771416
1
Just setretval = some words[0];
, start at index 1, and do just your else branch.
– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
add a comment |
1
Just setretval = some words[0];
, start at index 1, and do just your else branch.
– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
1
1
Just set
retval = some words[0];
, start at index 1, and do just your else branch.– Drew Reese
Nov 22 at 0:50
Just set
retval = some words[0];
, start at index 1, and do just your else branch.– Drew Reese
Nov 22 at 0:50
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
@DrewReese, it took me a moment to understand but you are so right! Changed it and I'll remember this comment!
– blaze_125
Nov 22 at 2:19
add a comment |
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%2f53422104%2flooping-over-javascript-object-and-adding-string-to-end-if-not-the-last-item%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
append the or at the beginning of the string when index > 0 instead of adding it at the end when index > -1
– blaze_125
Nov 22 at 0:19
The classic Fence Post problem. The basic algorithm is to start at your first post (element at index 0 in this case) and post-pend your fence (the
" OR "
) up until the second to last element, then add your last post. As others have pointed out, the array join function does this for you.– Drew Reese
Nov 22 at 0:53