Create an object from a string array The Next CEO of Stack OverflowWhat is the most efficient way to deep clone an object in JavaScript?Create ArrayList from arrayHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueHow to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Is it a bad idea to plug the other end of ESD strap to wall ground?
What difference does it make matching a word with/without a trailing whitespace?
Is it OK to decorate a log book cover?
How dangerous is XSS
Mathematica command that allows it to read my intentions
Masking layers by a vector polygon layer in QGIS
What does it mean 'exit 1' for a job status after rclone sync
Why does freezing point matter when picking cooler ice packs?
Is it possible to create a QR code using text?
Read/write a pipe-delimited file line by line with some simple text manipulation
Strange use of "whether ... than ..." in official text
Can I cast Thunderwave and be at the center of its bottom face, but not be affected by it?
Why did early computer designers eschew integers?
Avoiding the "not like other girls" trope?
Why can't we say "I have been having a dog"?
Calculate the Mean mean of two numbers
Can a PhD from a non-TU9 German university become a professor in a TU9 university?
How seriously should I take size and weight limits of hand luggage?
Can this transistor (2n2222) take 6V on emitter-base? Am I reading datasheet incorrectly?
What steps are necessary to read a Modern SSD in Medieval Europe?
Direct Implications Between USA and UK in Event of No-Deal Brexit
Simplify trigonometric expression using trigonometric identities
Does Germany produce more waste than the US?
Why do we say “un seul M” and not “une seule M” even though M is a “consonne”?
Create an object from a string array
The Next CEO of Stack OverflowWhat is the most efficient way to deep clone an object in JavaScript?Create ArrayList from arrayHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueHow to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I'm trying to create an object from a string array.
I've this string array :
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
and I would like to have an object like that :
origin : ['develop', 'master'],
toto : ['branch'],
tata : ['hello', 'world']
So for the moment, I did this :
let Obj = ;
let RemoteObj = ;
for (let CurrentIndex = 0; CurrentIndex < BaseArray.length; CurrentIndex++)
let Splits = BaseArray[CurrentIndex].split('/');
if (Splits[0] && Splits[1])
Obj[Splits[0]] = Splits[1].trim();
if (this.isObjectEmpty(RemoteObj))
RemoteObj = Obj;
else
RemoteObj = this.mergeObjects(RemoteObj, Obj);
console.log(RemoteObj);
And my utils functions are :
mergeObjects(...objs)
let Result = , Obj;
for (let Ind = 0, IndLen = objs.length; Ind < IndLen; Ind++)
Obj = objs[Ind];
for (let Prop in Obj)
if (Obj.hasOwnProperty(Prop))
if (!Result.hasOwnProperty(Prop))
Result[Prop] = [];
Result[Prop].push(Obj[Prop]);
return Result;
isObjectEmpty(Obj)
for (let Key in Obj)
if (Obj.hasOwnProperty(Key))
return false;
return true;
I'm sure there is a better solution to do it but I can't do it.
So I'm open to any help !
Thanks by advance !
javascript arrays typescript object
add a comment |
I'm trying to create an object from a string array.
I've this string array :
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
and I would like to have an object like that :
origin : ['develop', 'master'],
toto : ['branch'],
tata : ['hello', 'world']
So for the moment, I did this :
let Obj = ;
let RemoteObj = ;
for (let CurrentIndex = 0; CurrentIndex < BaseArray.length; CurrentIndex++)
let Splits = BaseArray[CurrentIndex].split('/');
if (Splits[0] && Splits[1])
Obj[Splits[0]] = Splits[1].trim();
if (this.isObjectEmpty(RemoteObj))
RemoteObj = Obj;
else
RemoteObj = this.mergeObjects(RemoteObj, Obj);
console.log(RemoteObj);
And my utils functions are :
mergeObjects(...objs)
let Result = , Obj;
for (let Ind = 0, IndLen = objs.length; Ind < IndLen; Ind++)
Obj = objs[Ind];
for (let Prop in Obj)
if (Obj.hasOwnProperty(Prop))
if (!Result.hasOwnProperty(Prop))
Result[Prop] = [];
Result[Prop].push(Obj[Prop]);
return Result;
isObjectEmpty(Obj)
for (let Key in Obj)
if (Obj.hasOwnProperty(Key))
return false;
return true;
I'm sure there is a better solution to do it but I can't do it.
So I'm open to any help !
Thanks by advance !
javascript arrays typescript object
add a comment |
I'm trying to create an object from a string array.
I've this string array :
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
and I would like to have an object like that :
origin : ['develop', 'master'],
toto : ['branch'],
tata : ['hello', 'world']
So for the moment, I did this :
let Obj = ;
let RemoteObj = ;
for (let CurrentIndex = 0; CurrentIndex < BaseArray.length; CurrentIndex++)
let Splits = BaseArray[CurrentIndex].split('/');
if (Splits[0] && Splits[1])
Obj[Splits[0]] = Splits[1].trim();
if (this.isObjectEmpty(RemoteObj))
RemoteObj = Obj;
else
RemoteObj = this.mergeObjects(RemoteObj, Obj);
console.log(RemoteObj);
And my utils functions are :
mergeObjects(...objs)
let Result = , Obj;
for (let Ind = 0, IndLen = objs.length; Ind < IndLen; Ind++)
Obj = objs[Ind];
for (let Prop in Obj)
if (Obj.hasOwnProperty(Prop))
if (!Result.hasOwnProperty(Prop))
Result[Prop] = [];
Result[Prop].push(Obj[Prop]);
return Result;
isObjectEmpty(Obj)
for (let Key in Obj)
if (Obj.hasOwnProperty(Key))
return false;
return true;
I'm sure there is a better solution to do it but I can't do it.
So I'm open to any help !
Thanks by advance !
javascript arrays typescript object
I'm trying to create an object from a string array.
I've this string array :
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
and I would like to have an object like that :
origin : ['develop', 'master'],
toto : ['branch'],
tata : ['hello', 'world']
So for the moment, I did this :
let Obj = ;
let RemoteObj = ;
for (let CurrentIndex = 0; CurrentIndex < BaseArray.length; CurrentIndex++)
let Splits = BaseArray[CurrentIndex].split('/');
if (Splits[0] && Splits[1])
Obj[Splits[0]] = Splits[1].trim();
if (this.isObjectEmpty(RemoteObj))
RemoteObj = Obj;
else
RemoteObj = this.mergeObjects(RemoteObj, Obj);
console.log(RemoteObj);
And my utils functions are :
mergeObjects(...objs)
let Result = , Obj;
for (let Ind = 0, IndLen = objs.length; Ind < IndLen; Ind++)
Obj = objs[Ind];
for (let Prop in Obj)
if (Obj.hasOwnProperty(Prop))
if (!Result.hasOwnProperty(Prop))
Result[Prop] = [];
Result[Prop].push(Obj[Prop]);
return Result;
isObjectEmpty(Obj)
for (let Key in Obj)
if (Obj.hasOwnProperty(Key))
return false;
return true;
I'm sure there is a better solution to do it but I can't do it.
So I'm open to any help !
Thanks by advance !
javascript arrays typescript object
javascript arrays typescript object
asked Mar 8 at 19:15
NuljiNulji
825
825
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can use Array.reduce() to create the object by splitting each string to the key and value, assigning an empty array to the key if it doesn't exist, and pushing the value to the array:
const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is likeorigin/abc/defthen it will take onlyabc, instead ofabc&def
– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
add a comment |
You can use Array.reduce() for this approach. On each iteration of reduce you can split your string by / and use the first element as a key on the new object and then put the second element on the array associated with that key:
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;add a comment |
You can use split and reduce
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)add a comment |
You can use the reduce method to build your object.
let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);add a comment |
You can use reduce & split the string which will give an array. Then use the element at index 0 of the array to create the object key. And push rest of the value to the array
let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)add a comment |
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%2f55069607%2fcreate-an-object-from-a-string-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Array.reduce() to create the object by splitting each string to the key and value, assigning an empty array to the key if it doesn't exist, and pushing the value to the array:
const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is likeorigin/abc/defthen it will take onlyabc, instead ofabc&def
– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
add a comment |
You can use Array.reduce() to create the object by splitting each string to the key and value, assigning an empty array to the key if it doesn't exist, and pushing the value to the array:
const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is likeorigin/abc/defthen it will take onlyabc, instead ofabc&def
– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
add a comment |
You can use Array.reduce() to create the object by splitting each string to the key and value, assigning an empty array to the key if it doesn't exist, and pushing the value to the array:
const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);You can use Array.reduce() to create the object by splitting each string to the key and value, assigning an empty array to the key if it doesn't exist, and pushing the value to the array:
const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);const BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
const result = BaseArray.reduce((r, str) =>
const [key, value] = str.split('/');
if(!r[key]) r[key] = [];
r[key].push(value);
return r;
, );
console.log(result);answered Mar 8 at 19:17
Ori DroriOri Drori
81.4k138997
81.4k138997
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is likeorigin/abc/defthen it will take onlyabc, instead ofabc&def
– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
add a comment |
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is likeorigin/abc/defthen it will take onlyabc, instead ofabc&def
– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
Thanks! It works perfectly! I tried at the beginning with reduce but I failed due to a bad usage.
– Nulji
Mar 8 at 19:22
1
1
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
You're welcome. As a rule of thumb - an array to object transformation can usually be done with reduce.
– Ori Drori
Mar 8 at 19:23
@OriDrori can we use destructuring assignment to get all the values. For example if a string is like
origin/abc/def then it will take only abc, instead of abc &def– brk
Mar 8 at 19:49
@OriDrori can we use destructuring assignment to get all the values. For example if a string is like
origin/abc/def then it will take only abc, instead of abc &def– brk
Mar 8 at 19:49
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
If it's a case of nested objects, I wouldn't use destructuring. I would split the path, and then iterate the array of path parts with another loop to build the structure.
– Ori Drori
Mar 8 at 19:53
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
@brk if all of them need to be added in array ['abc','def'] like this than you can use rest parameter
– Code Maniac
Mar 8 at 19:55
add a comment |
You can use Array.reduce() for this approach. On each iteration of reduce you can split your string by / and use the first element as a key on the new object and then put the second element on the array associated with that key:
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;add a comment |
You can use Array.reduce() for this approach. On each iteration of reduce you can split your string by / and use the first element as a key on the new object and then put the second element on the array associated with that key:
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;add a comment |
You can use Array.reduce() for this approach. On each iteration of reduce you can split your string by / and use the first element as a key on the new object and then put the second element on the array associated with that key:
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;You can use Array.reduce() for this approach. On each iteration of reduce you can split your string by / and use the first element as a key on the new object and then put the second element on the array associated with that key:
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let res = BaseArray.reduce((acc, curr) =>
, );
console.log(res);.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;edited Mar 8 at 19:25
answered Mar 8 at 19:19
ShiderszShidersz
9,4382933
9,4382933
add a comment |
add a comment |
You can use split and reduce
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)add a comment |
You can use split and reduce
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)add a comment |
You can use split and reduce
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)You can use split and reduce
let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)let BaseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let op = BaseArray.reduce((op, inp) =>
let [key, value] = inp.split('/')
op[key] = op[key] ,)
console.log(op)edited Mar 8 at 19:29
answered Mar 8 at 19:18
Code ManiacCode Maniac
11k2733
11k2733
add a comment |
add a comment |
You can use the reduce method to build your object.
let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);add a comment |
You can use the reduce method to build your object.
let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);add a comment |
You can use the reduce method to build your object.
let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);You can use the reduce method to build your object.
let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);let baseArray = ['origin/develop', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let baseobj = baseArray.reduce((acc, curr) =>
let items = curr.split('/');
let key = items[0];
let value = items[1];
if(acc[key] === undefined)
acc[key] = [value]
else
acc[key] = [...acc[key], value];
return acc;
, );
console.log(baseobj);answered Mar 8 at 19:21
Sushanth --Sushanth --
50.6k64885
50.6k64885
add a comment |
add a comment |
You can use reduce & split the string which will give an array. Then use the element at index 0 of the array to create the object key. And push rest of the value to the array
let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)add a comment |
You can use reduce & split the string which will give an array. Then use the element at index 0 of the array to create the object key. And push rest of the value to the array
let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)add a comment |
You can use reduce & split the string which will give an array. Then use the element at index 0 of the array to create the object key. And push rest of the value to the array
let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)You can use reduce & split the string which will give an array. Then use the element at index 0 of the array to create the object key. And push rest of the value to the array
let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)let BaseArray = ['origin/develop', 'origin/kit/sub', 'origin/master', 'toto/branch', 'tata/hello', 'tata/world'];
let newArray = BaseArray.reduce(function(acc, curr)
let splitCurr = curr.split('/');
if (!acc[splitCurr[0]])
acc[splitCurr[0]] = []
for (let i = 1; i < splitCurr.length; i++)
acc[splitCurr[0]].push(splitCurr[i])
return acc;
, );
console.log(newArray)answered Mar 8 at 19:27
brkbrk
29.5k32244
29.5k32244
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%2f55069607%2fcreate-an-object-from-a-string-array%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
