Sort Object (string) by another object
I currently have one object which looks like this:
var data = {
"a/a/something": "something/ehere"
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
This Object.keys
currently is segmented with 3 elements, and all i care about is the first and third [1]/[2]/[3]
.
I am currently running a foreach
that loops through all the object keys but i want to sort it by the criteria 1 and criteria 3.
My Loop:
Object.keys(data).forEach((dataID) => {
register(dataID);
});
My sort:
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
The objective is so it would check whether key a/a/something
and when it reaches the loop, it should check the sort object before it hits the foreach.
Expected result prior to forEach
var data = {
"a/a/something": "something/ehere"
"a/b/somethingdifferent": "something/else/different",
"a/b/somethingelse": "something/else/here",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
"b/a/somethingdifferent": "something/else/somethingess"
};
javascript arrays sorting object
add a comment |
I currently have one object which looks like this:
var data = {
"a/a/something": "something/ehere"
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
This Object.keys
currently is segmented with 3 elements, and all i care about is the first and third [1]/[2]/[3]
.
I am currently running a foreach
that loops through all the object keys but i want to sort it by the criteria 1 and criteria 3.
My Loop:
Object.keys(data).forEach((dataID) => {
register(dataID);
});
My sort:
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
The objective is so it would check whether key a/a/something
and when it reaches the loop, it should check the sort object before it hits the foreach.
Expected result prior to forEach
var data = {
"a/a/something": "something/ehere"
"a/b/somethingdifferent": "something/else/different",
"a/b/somethingelse": "something/else/here",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
"b/a/somethingdifferent": "something/else/somethingess"
};
javascript arrays sorting object
2
What exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
1
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
Its not clear what you want but you should probably.split("/")
somewhere
– JSelser
Nov 12 '18 at 19:59
add a comment |
I currently have one object which looks like this:
var data = {
"a/a/something": "something/ehere"
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
This Object.keys
currently is segmented with 3 elements, and all i care about is the first and third [1]/[2]/[3]
.
I am currently running a foreach
that loops through all the object keys but i want to sort it by the criteria 1 and criteria 3.
My Loop:
Object.keys(data).forEach((dataID) => {
register(dataID);
});
My sort:
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
The objective is so it would check whether key a/a/something
and when it reaches the loop, it should check the sort object before it hits the foreach.
Expected result prior to forEach
var data = {
"a/a/something": "something/ehere"
"a/b/somethingdifferent": "something/else/different",
"a/b/somethingelse": "something/else/here",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
"b/a/somethingdifferent": "something/else/somethingess"
};
javascript arrays sorting object
I currently have one object which looks like this:
var data = {
"a/a/something": "something/ehere"
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
This Object.keys
currently is segmented with 3 elements, and all i care about is the first and third [1]/[2]/[3]
.
I am currently running a foreach
that loops through all the object keys but i want to sort it by the criteria 1 and criteria 3.
My Loop:
Object.keys(data).forEach((dataID) => {
register(dataID);
});
My sort:
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
The objective is so it would check whether key a/a/something
and when it reaches the loop, it should check the sort object before it hits the foreach.
Expected result prior to forEach
var data = {
"a/a/something": "something/ehere"
"a/b/somethingdifferent": "something/else/different",
"a/b/somethingelse": "something/else/here",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
"b/a/somethingdifferent": "something/else/somethingess"
};
javascript arrays sorting object
javascript arrays sorting object
edited Nov 12 '18 at 19:37
asked Nov 12 '18 at 19:23
Sophie Rhodes
879922
879922
2
What exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
1
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
Its not clear what you want but you should probably.split("/")
somewhere
– JSelser
Nov 12 '18 at 19:59
add a comment |
2
What exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
1
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
Its not clear what you want but you should probably.split("/")
somewhere
– JSelser
Nov 12 '18 at 19:59
2
2
What exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
What exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
1
1
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
Its not clear what you want but you should probably
.split("/")
somewhere– JSelser
Nov 12 '18 at 19:59
Its not clear what you want but you should probably
.split("/")
somewhere– JSelser
Nov 12 '18 at 19:59
add a comment |
3 Answers
3
active
oldest
votes
You can try below approach to handle your scenario
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
add a comment |
You could do something like this:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
The idea is to get the keys via Object.keys
then group them by index [0]
, sort them and then compose the final object.
Note: Object props order is not guaranteed:
An object is a member of the type Object. It is an unordered
collection of properties each of which contains a primitive value,
object, or function. A function stored in a property of an object is
called a method.
Although the SO console would show the desired order in Chrome console you would not get the same output. Just FYI.
So with that in mind if you replace the last line and instead of reduce
you do map
so the end result is an array:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
You would get a guaranteed index order and the correct / recommended output. You can not rely on object prop order.
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
Sorry from your question the impression is that you would take the firstdata
and sort it using the[0]/[1]/[2]
indexes. So this takes the input fromdata
and taking the keys sorts them by only coparing[0]/[2]
indexes. If this is not correct let me know.
– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.var sort
should be what my sorting should be based on, the idea behind is that i currently have"a"
object which has an array of how it should sort,. However, to split it up, you need[0]
and[2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
add a comment |
Objects in javascript are not sorted, so you cannot sort keys. However, let's say you wanted to end up with a sorted array of the keys...
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
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%2f53268776%2fsort-object-string-by-another-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try below approach to handle your scenario
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
add a comment |
You can try below approach to handle your scenario
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
add a comment |
You can try below approach to handle your scenario
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
You can try below approach to handle your scenario
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
var data = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent",
};
var sort = {
"a": ["something", "somethingdifferent", "somethingelse"],
"b": ["somethingess", "somethingdifferentasdasd"]
}
let tmpData = {}
for(let [key, value] of Object.entries(data)) {
tmpData[key.split('/')[0]] = tmpData[key.split('/')[0]] || {}
tmpData[key.split('/')[0]][key.split('/')[2]] = { [key]: value, value: 999 }
}
let tmpResult = {}
for(let [key, args] of Object.entries(sort)) {
args.forEach((d,i) => tmpData[key][d]['value'] = i)
Object.assign(tmpResult, ... Object.entries(tmpData[key]).map(([, v]) => v).sort((a, b) => a.value - b.value))
}
let {value, ...result} = tmpResult
console.log(result)
answered Nov 12 '18 at 21:09
Nitish Narang
2,938815
2,938815
add a comment |
add a comment |
You could do something like this:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
The idea is to get the keys via Object.keys
then group them by index [0]
, sort them and then compose the final object.
Note: Object props order is not guaranteed:
An object is a member of the type Object. It is an unordered
collection of properties each of which contains a primitive value,
object, or function. A function stored in a property of an object is
called a method.
Although the SO console would show the desired order in Chrome console you would not get the same output. Just FYI.
So with that in mind if you replace the last line and instead of reduce
you do map
so the end result is an array:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
You would get a guaranteed index order and the correct / recommended output. You can not rely on object prop order.
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
Sorry from your question the impression is that you would take the firstdata
and sort it using the[0]/[1]/[2]
indexes. So this takes the input fromdata
and taking the keys sorts them by only coparing[0]/[2]
indexes. If this is not correct let me know.
– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.var sort
should be what my sorting should be based on, the idea behind is that i currently have"a"
object which has an array of how it should sort,. However, to split it up, you need[0]
and[2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
add a comment |
You could do something like this:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
The idea is to get the keys via Object.keys
then group them by index [0]
, sort them and then compose the final object.
Note: Object props order is not guaranteed:
An object is a member of the type Object. It is an unordered
collection of properties each of which contains a primitive value,
object, or function. A function stored in a property of an object is
called a method.
Although the SO console would show the desired order in Chrome console you would not get the same output. Just FYI.
So with that in mind if you replace the last line and instead of reduce
you do map
so the end result is an array:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
You would get a guaranteed index order and the correct / recommended output. You can not rely on object prop order.
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
Sorry from your question the impression is that you would take the firstdata
and sort it using the[0]/[1]/[2]
indexes. So this takes the input fromdata
and taking the keys sorts them by only coparing[0]/[2]
indexes. If this is not correct let me know.
– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.var sort
should be what my sorting should be based on, the idea behind is that i currently have"a"
object which has an array of how it should sort,. However, to split it up, you need[0]
and[2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
add a comment |
You could do something like this:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
The idea is to get the keys via Object.keys
then group them by index [0]
, sort them and then compose the final object.
Note: Object props order is not guaranteed:
An object is a member of the type Object. It is an unordered
collection of properties each of which contains a primitive value,
object, or function. A function stored in a property of an object is
called a method.
Although the SO console would show the desired order in Chrome console you would not get the same output. Just FYI.
So with that in mind if you replace the last line and instead of reduce
you do map
so the end result is an array:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
You would get a guaranteed index order and the correct / recommended output. You can not rely on object prop order.
You could do something like this:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
The idea is to get the keys via Object.keys
then group them by index [0]
, sort them and then compose the final object.
Note: Object props order is not guaranteed:
An object is a member of the type Object. It is an unordered
collection of properties each of which contains a primitive value,
object, or function. A function stored in a property of an object is
called a method.
Although the SO console would show the desired order in Chrome console you would not get the same output. Just FYI.
So with that in mind if you replace the last line and instead of reduce
you do map
so the end result is an array:
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
You would get a guaranteed index order and the correct / recommended output. You can not rely on object prop order.
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.reduce((r, c) => Object.assign(r, ({ [c]: data[c]})), {})
console.log(result)
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
const data = { "a/a/something": "something/ehere", "a/b/somethingelse": "something/else/here", "a/b/somethingdifferent": "something/else/different", "a/c/somethingess": "something/else/somethingess", "b/a/somethingess": "something/else/somethingess", "b/a/somethingdifferent": "something/else/somethingess", "b/b/somethingdifferentasdasd": "something/else/somethingdifferent", };
const sort = { "a": ["something", "somethingdifferent", "somethingelse"], "b": ["somethingess", "somethingdifferentasdasd"] }
const splitter = (s) => {
let arr = s.split('/'), index = sort[arr[0]].indexOf(arr[2])
return ({ k: arr[0], v: index < 0 ? Number.MAX_SAFE_INTEGER : index})
}
const sorter = (a, b) => splitter(a).v - splitter(b).v
const result = Object.entries(
Object.keys(data)
.reduce((r, c) => (r[splitter(c).k] = [...r[splitter(c).k] || , c], r), {})
)
.map(([k, v]) => v.sort(sorter))
.reduce((r, c) => r.concat(c))
.map(x => ({[x] : data[x]}))
console.log(result)
edited Nov 12 '18 at 23:40
answered Nov 12 '18 at 19:54
Akrion
9,40711224
9,40711224
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
Sorry from your question the impression is that you would take the firstdata
and sort it using the[0]/[1]/[2]
indexes. So this takes the input fromdata
and taking the keys sorts them by only coparing[0]/[2]
indexes. If this is not correct let me know.
– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.var sort
should be what my sorting should be based on, the idea behind is that i currently have"a"
object which has an array of how it should sort,. However, to split it up, you need[0]
and[2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
add a comment |
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
Sorry from your question the impression is that you would take the firstdata
and sort it using the[0]/[1]/[2]
indexes. So this takes the input fromdata
and taking the keys sorts them by only coparing[0]/[2]
indexes. If this is not correct let me know.
– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.var sort
should be what my sorting should be based on, the idea behind is that i currently have"a"
object which has an array of how it should sort,. However, to split it up, you need[0]
and[2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
Thank you, question, where in this are you referencing the sort object? how does it know how to sort
– Sophie Rhodes
Nov 12 '18 at 20:14
1
1
Sorry from your question the impression is that you would take the first
data
and sort it using the [0]/[1]/[2]
indexes. So this takes the input from data
and taking the keys sorts them by only coparing [0]/[2]
indexes. If this is not correct let me know.– Akrion
Nov 12 '18 at 20:16
Sorry from your question the impression is that you would take the first
data
and sort it using the [0]/[1]/[2]
indexes. So this takes the input from data
and taking the keys sorts them by only coparing [0]/[2]
indexes. If this is not correct let me know.– Akrion
Nov 12 '18 at 20:16
Sorry Akrion, i think i may have confused everyone.
var sort
should be what my sorting should be based on, the idea behind is that i currently have "a"
object which has an array of how it should sort,. However, to split it up, you need [0]
and [2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Sorry Akrion, i think i may have confused everyone.
var sort
should be what my sorting should be based on, the idea behind is that i currently have "a"
object which has an array of how it should sort,. However, to split it up, you need [0]
and [2]
– Sophie Rhodes
Nov 12 '18 at 20:19
Updated the answer
– Akrion
Nov 12 '18 at 22:11
Updated the answer
– Akrion
Nov 12 '18 at 22:11
add a comment |
Objects in javascript are not sorted, so you cannot sort keys. However, let's say you wanted to end up with a sorted array of the keys...
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
add a comment |
Objects in javascript are not sorted, so you cannot sort keys. However, let's say you wanted to end up with a sorted array of the keys...
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
add a comment |
Objects in javascript are not sorted, so you cannot sort keys. However, let's say you wanted to end up with a sorted array of the keys...
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
Objects in javascript are not sorted, so you cannot sort keys. However, let's say you wanted to end up with a sorted array of the keys...
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
const inputData = {
"a/a/something": "something/ehere",
"a/b/somethingelse": "something/else/here",
"a/b/somethingdifferent": "something/else/different",
"a/c/somethingess": "something/else/somethingess",
"b/a/somethingess": "something/else/somethingess",
"b/a/somethingdifferent": "something/else/somethingess",
"b/b/somethingdifferentasdasd": "something/else/somethingdifferent"
};
const sortingFunction = (x, y) => {
let [a, b, c] = x.split('/');
let [A, B, C] = y.split('/');
if (a < A) {
return -1;
} else if (A < a) {
return 1;
} else {
if (c < C) {
return -1;
} else if (C < c) {
return 1;
} else {
return 0;
}
}
};
const sortedKeys = Object.keys(inputData);
sortedKeys.sort(sortingFunction);
console.log(sortedKeys);
answered Nov 12 '18 at 19:56
code_monk
3,84412829
3,84412829
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53268776%2fsort-object-string-by-another-object%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 exactly is the expected output?
– Akrion
Nov 12 '18 at 19:31
1
@Akrion, just added in the expected result prior to foreach
– Sophie Rhodes
Nov 12 '18 at 19:38
Its not clear what you want but you should probably
.split("/")
somewhere– JSelser
Nov 12 '18 at 19:59