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?










4















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 !










share|improve this question


























    4















    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 !










    share|improve this question
























      4












      4








      4








      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 !










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 19:15









      NuljiNulji

      825




      825






















          5 Answers
          5






          active

          oldest

          votes


















          6














          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);








          share|improve this answer























          • 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 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











          • @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



















          3














          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;








          share|improve this answer
































            3














            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)








            share|improve this answer
































              2














              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);








              share|improve this answer






























                1














                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)








                share|improve this answer























                  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
                  );



                  );













                  draft saved

                  draft discarded


















                  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









                  6














                  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);








                  share|improve this answer























                  • 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 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











                  • @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
















                  6














                  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);








                  share|improve this answer























                  • 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 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











                  • @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














                  6












                  6








                  6







                  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);








                  share|improve this answer













                  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);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  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 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











                  • @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






                  • 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 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











                  • @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














                  3














                  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;








                  share|improve this answer





























                    3














                    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;








                    share|improve this answer



























                      3












                      3








                      3







                      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;








                      share|improve this answer















                      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;






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 8 at 19:25

























                      answered Mar 8 at 19:19









                      ShiderszShidersz

                      9,4382933




                      9,4382933





















                          3














                          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)








                          share|improve this answer





























                            3














                            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)








                            share|improve this answer



























                              3












                              3








                              3







                              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)








                              share|improve this answer















                              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)






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 8 at 19:29

























                              answered Mar 8 at 19:18









                              Code ManiacCode Maniac

                              11k2733




                              11k2733





















                                  2














                                  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);








                                  share|improve this answer



























                                    2














                                    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);








                                    share|improve this answer

























                                      2












                                      2








                                      2







                                      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);








                                      share|improve this answer













                                      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);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 8 at 19:21









                                      Sushanth --Sushanth --

                                      50.6k64885




                                      50.6k64885





















                                          1














                                          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)








                                          share|improve this answer



























                                            1














                                            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)








                                            share|improve this answer

























                                              1












                                              1








                                              1







                                              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)








                                              share|improve this answer













                                              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)






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Mar 8 at 19:27









                                              brkbrk

                                              29.5k32244




                                              29.5k32244



























                                                  draft saved

                                                  draft discarded
















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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







                                                  Popular posts from this blog

                                                  How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                                  Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                                  List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229