How to replace a value of a key in a object












0















Him I am new to the react redux. I have an array of objects which is like,



let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'}{Id :'a',temp:5,data:'xyz (c:1'}]


Now, what I want to do is that there is key called data with some value, I want to take the value which is before that bracket only in that data key.



what I want to look an object ,



{Id :'a',count:5,data:'abc'}


what I have is ,
I have an regex which I use to get that value,



and I have used for loop ,



for (let i = 0; i <= result.length - 1; i++) {
let regex = /^[^(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value
}


fetchQuestions() {
let previous_data = { ...this.props.data };
let result = ;
for (let key in previous_data) {
result = [...result, ...previous_data[key]];
}
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)(/;
let value = result[i].technology.match(regex)[1].trim();
result[i].technology = value;
}
this.props.fetchQuestions(result);
}


In this this.props.data is the reducer data .Now what I want to do was, I have to get some data from this and do some manipulation. but here, it is directly updating my reducer values itself, But I have used spread operator to get the copy of that data ,but still its updating the reducers values.



But I am not getting the value which I expect. Can any one help me with this ?










share|improve this question

























  • try this - let regex =/^(w+)/;

    – Nikita R.
    Nov 14 '18 at 6:21
















0















Him I am new to the react redux. I have an array of objects which is like,



let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'}{Id :'a',temp:5,data:'xyz (c:1'}]


Now, what I want to do is that there is key called data with some value, I want to take the value which is before that bracket only in that data key.



what I want to look an object ,



{Id :'a',count:5,data:'abc'}


what I have is ,
I have an regex which I use to get that value,



and I have used for loop ,



for (let i = 0; i <= result.length - 1; i++) {
let regex = /^[^(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value
}


fetchQuestions() {
let previous_data = { ...this.props.data };
let result = ;
for (let key in previous_data) {
result = [...result, ...previous_data[key]];
}
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)(/;
let value = result[i].technology.match(regex)[1].trim();
result[i].technology = value;
}
this.props.fetchQuestions(result);
}


In this this.props.data is the reducer data .Now what I want to do was, I have to get some data from this and do some manipulation. but here, it is directly updating my reducer values itself, But I have used spread operator to get the copy of that data ,but still its updating the reducers values.



But I am not getting the value which I expect. Can any one help me with this ?










share|improve this question

























  • try this - let regex =/^(w+)/;

    – Nikita R.
    Nov 14 '18 at 6:21














0












0








0








Him I am new to the react redux. I have an array of objects which is like,



let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'}{Id :'a',temp:5,data:'xyz (c:1'}]


Now, what I want to do is that there is key called data with some value, I want to take the value which is before that bracket only in that data key.



what I want to look an object ,



{Id :'a',count:5,data:'abc'}


what I have is ,
I have an regex which I use to get that value,



and I have used for loop ,



for (let i = 0; i <= result.length - 1; i++) {
let regex = /^[^(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value
}


fetchQuestions() {
let previous_data = { ...this.props.data };
let result = ;
for (let key in previous_data) {
result = [...result, ...previous_data[key]];
}
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)(/;
let value = result[i].technology.match(regex)[1].trim();
result[i].technology = value;
}
this.props.fetchQuestions(result);
}


In this this.props.data is the reducer data .Now what I want to do was, I have to get some data from this and do some manipulation. but here, it is directly updating my reducer values itself, But I have used spread operator to get the copy of that data ,but still its updating the reducers values.



But I am not getting the value which I expect. Can any one help me with this ?










share|improve this question
















Him I am new to the react redux. I have an array of objects which is like,



let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'}{Id :'a',temp:5,data:'xyz (c:1'}]


Now, what I want to do is that there is key called data with some value, I want to take the value which is before that bracket only in that data key.



what I want to look an object ,



{Id :'a',count:5,data:'abc'}


what I have is ,
I have an regex which I use to get that value,



and I have used for loop ,



for (let i = 0; i <= result.length - 1; i++) {
let regex = /^[^(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value
}


fetchQuestions() {
let previous_data = { ...this.props.data };
let result = ;
for (let key in previous_data) {
result = [...result, ...previous_data[key]];
}
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)(/;
let value = result[i].technology.match(regex)[1].trim();
result[i].technology = value;
}
this.props.fetchQuestions(result);
}


In this this.props.data is the reducer data .Now what I want to do was, I have to get some data from this and do some manipulation. but here, it is directly updating my reducer values itself, But I have used spread operator to get the copy of that data ,but still its updating the reducers values.



But I am not getting the value which I expect. Can any one help me with this ?







javascript reactjs redux react-redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 8:55







ganeshk

















asked Nov 14 '18 at 6:14









ganeshkganeshk

24219




24219













  • try this - let regex =/^(w+)/;

    – Nikita R.
    Nov 14 '18 at 6:21



















  • try this - let regex =/^(w+)/;

    – Nikita R.
    Nov 14 '18 at 6:21

















try this - let regex =/^(w+)/;

– Nikita R.
Nov 14 '18 at 6:21





try this - let regex =/^(w+)/;

– Nikita R.
Nov 14 '18 at 6:21












5 Answers
5






active

oldest

votes


















2














You can write a regex that creates a group for the value before ( and then get it from the match






let result= [
{
Id: 'a',
temp: 5,
data: 'abc (c:1'
},
{
Id: 'a',
temp: 5,
data: 'pqr (c:1'
},
{
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}
]
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)(/;
let value = result[i].data.match(regex)[1].trim();
result[i].data = value
}

console.log(result);








share|improve this answer


























  • Using this regex I am getting a bracket in the data value

    – ganeshk
    Nov 14 '18 at 6:24











  • and If I use 1 then it gives null. I think it should be 0

    – ganeshk
    Nov 14 '18 at 6:25











  • Hey @shubham I got so many solutions, Which one will be more efficient

    – ganeshk
    Nov 14 '18 at 6:29











  • @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

    – Shubham Khatri
    Nov 14 '18 at 6:31











  • I have just updated my question,On reducer value is getting updated , But It should not get update with

    – ganeshk
    Nov 14 '18 at 8:53



















1














You need to access the data property of each object while you're iterating, in order to use a .match on it:






let result = [{
Id: 'a',
temp: 5,
data: 'abc (c:1'
}, {
Id: 'a',
temp: 5,
data: 'pqr (c:1'
}, {
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}];
for (let i = 0; i <= result.length - 1; i++) {
const { data } = result[i];
let regex = /^[^(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value;
}
console.log(result);





But, seeing as you're using react, you might consider using .map instead, which won't mutate the original objects:






let result = [{
Id: 'a',
temp: 5,
data: 'abc (c:1'
}, {
Id: 'a',
temp: 5,
data: 'pqr (c:1'
}, {
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}];
console.log(
result.map(({ data, ...rest }) => ({
data: data.match(/^[^(]+[^( ]/)[0],
...rest
}))
);





Note that if you make the final matched character in the regex [^( ] (match anything but a ( or a space), there's no need for trim anymore.






share|improve this answer































    0














    You can use split('(') instead of regex to get the same output:






    let result = [{
    Id: 'a',
    temp: 5,
    data: 'abc (c:1'
    }, {
    Id: 'a',
    temp: 5,
    data: 'pqr (c:1'
    }, {
    Id: 'a',
    temp: 5,
    data: 'xyz (c:1'
    }];
    for (let i = 0; i <= result.length - 1; i++) {
    let value = result[i].data.split('(')[0].trim();
    result[i].data = value
    }
    console.log(result);








    share|improve this answer































      0














      Try this one.



        result.map(el => {
      el.data = el.data.match(/^(w+)/)[0];
      return el;
      })





      share|improve this answer































        0














        let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]

        const new_result = result.map((item) => {
        let regex = /^[^(]+/;
        let value = item.data.match(regex)[0].trim();
        item.data = value;
        return item;
        });

        console.log(new_result);





        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%2f53294158%2fhow-to-replace-a-value-of-a-key-in-a-object%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









          2














          You can write a regex that creates a group for the value before ( and then get it from the match






          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);








          share|improve this answer


























          • Using this regex I am getting a bracket in the data value

            – ganeshk
            Nov 14 '18 at 6:24











          • and If I use 1 then it gives null. I think it should be 0

            – ganeshk
            Nov 14 '18 at 6:25











          • Hey @shubham I got so many solutions, Which one will be more efficient

            – ganeshk
            Nov 14 '18 at 6:29











          • @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

            – Shubham Khatri
            Nov 14 '18 at 6:31











          • I have just updated my question,On reducer value is getting updated , But It should not get update with

            – ganeshk
            Nov 14 '18 at 8:53
















          2














          You can write a regex that creates a group for the value before ( and then get it from the match






          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);








          share|improve this answer


























          • Using this regex I am getting a bracket in the data value

            – ganeshk
            Nov 14 '18 at 6:24











          • and If I use 1 then it gives null. I think it should be 0

            – ganeshk
            Nov 14 '18 at 6:25











          • Hey @shubham I got so many solutions, Which one will be more efficient

            – ganeshk
            Nov 14 '18 at 6:29











          • @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

            – Shubham Khatri
            Nov 14 '18 at 6:31











          • I have just updated my question,On reducer value is getting updated , But It should not get update with

            – ganeshk
            Nov 14 '18 at 8:53














          2












          2








          2







          You can write a regex that creates a group for the value before ( and then get it from the match






          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);








          share|improve this answer















          You can write a regex that creates a group for the value before ( and then get it from the match






          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);








          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);





          let result= [
          {
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          },
          {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }
          ]
          for (let i = 0; i <= result.length - 1; i++) {
          let regex = /^(.*)(/;
          let value = result[i].data.match(regex)[1].trim();
          result[i].data = value
          }

          console.log(result);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 6:33

























          answered Nov 14 '18 at 6:19









          Shubham KhatriShubham Khatri

          81.8k1599138




          81.8k1599138













          • Using this regex I am getting a bracket in the data value

            – ganeshk
            Nov 14 '18 at 6:24











          • and If I use 1 then it gives null. I think it should be 0

            – ganeshk
            Nov 14 '18 at 6:25











          • Hey @shubham I got so many solutions, Which one will be more efficient

            – ganeshk
            Nov 14 '18 at 6:29











          • @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

            – Shubham Khatri
            Nov 14 '18 at 6:31











          • I have just updated my question,On reducer value is getting updated , But It should not get update with

            – ganeshk
            Nov 14 '18 at 8:53



















          • Using this regex I am getting a bracket in the data value

            – ganeshk
            Nov 14 '18 at 6:24











          • and If I use 1 then it gives null. I think it should be 0

            – ganeshk
            Nov 14 '18 at 6:25











          • Hey @shubham I got so many solutions, Which one will be more efficient

            – ganeshk
            Nov 14 '18 at 6:29











          • @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

            – Shubham Khatri
            Nov 14 '18 at 6:31











          • I have just updated my question,On reducer value is getting updated , But It should not get update with

            – ganeshk
            Nov 14 '18 at 8:53

















          Using this regex I am getting a bracket in the data value

          – ganeshk
          Nov 14 '18 at 6:24





          Using this regex I am getting a bracket in the data value

          – ganeshk
          Nov 14 '18 at 6:24













          and If I use 1 then it gives null. I think it should be 0

          – ganeshk
          Nov 14 '18 at 6:25





          and If I use 1 then it gives null. I think it should be 0

          – ganeshk
          Nov 14 '18 at 6:25













          Hey @shubham I got so many solutions, Which one will be more efficient

          – ganeshk
          Nov 14 '18 at 6:29





          Hey @shubham I got so many solutions, Which one will be more efficient

          – ganeshk
          Nov 14 '18 at 6:29













          @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

          – Shubham Khatri
          Nov 14 '18 at 6:31





          @ganeshk, the index will be 1 to get the first group match. Also a regex solution will be more efficient than a string manipulation using split. Any regex from the solutions that satisfies your use case should be good enough for you

          – Shubham Khatri
          Nov 14 '18 at 6:31













          I have just updated my question,On reducer value is getting updated , But It should not get update with

          – ganeshk
          Nov 14 '18 at 8:53





          I have just updated my question,On reducer value is getting updated , But It should not get update with

          – ganeshk
          Nov 14 '18 at 8:53













          1














          You need to access the data property of each object while you're iterating, in order to use a .match on it:






          let result = [{
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          }, {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          }, {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }];
          for (let i = 0; i <= result.length - 1; i++) {
          const { data } = result[i];
          let regex = /^[^(]+/;
          let value = data.match(regex)[0].trim();
          result[i].data = value;
          }
          console.log(result);





          But, seeing as you're using react, you might consider using .map instead, which won't mutate the original objects:






          let result = [{
          Id: 'a',
          temp: 5,
          data: 'abc (c:1'
          }, {
          Id: 'a',
          temp: 5,
          data: 'pqr (c:1'
          }, {
          Id: 'a',
          temp: 5,
          data: 'xyz (c:1'
          }];
          console.log(
          result.map(({ data, ...rest }) => ({
          data: data.match(/^[^(]+[^( ]/)[0],
          ...rest
          }))
          );





          Note that if you make the final matched character in the regex [^( ] (match anything but a ( or a space), there's no need for trim anymore.






          share|improve this answer




























            1














            You need to access the data property of each object while you're iterating, in order to use a .match on it:






            let result = [{
            Id: 'a',
            temp: 5,
            data: 'abc (c:1'
            }, {
            Id: 'a',
            temp: 5,
            data: 'pqr (c:1'
            }, {
            Id: 'a',
            temp: 5,
            data: 'xyz (c:1'
            }];
            for (let i = 0; i <= result.length - 1; i++) {
            const { data } = result[i];
            let regex = /^[^(]+/;
            let value = data.match(regex)[0].trim();
            result[i].data = value;
            }
            console.log(result);





            But, seeing as you're using react, you might consider using .map instead, which won't mutate the original objects:






            let result = [{
            Id: 'a',
            temp: 5,
            data: 'abc (c:1'
            }, {
            Id: 'a',
            temp: 5,
            data: 'pqr (c:1'
            }, {
            Id: 'a',
            temp: 5,
            data: 'xyz (c:1'
            }];
            console.log(
            result.map(({ data, ...rest }) => ({
            data: data.match(/^[^(]+[^( ]/)[0],
            ...rest
            }))
            );





            Note that if you make the final matched character in the regex [^( ] (match anything but a ( or a space), there's no need for trim anymore.






            share|improve this answer


























              1












              1








              1







              You need to access the data property of each object while you're iterating, in order to use a .match on it:






              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              for (let i = 0; i <= result.length - 1; i++) {
              const { data } = result[i];
              let regex = /^[^(]+/;
              let value = data.match(regex)[0].trim();
              result[i].data = value;
              }
              console.log(result);





              But, seeing as you're using react, you might consider using .map instead, which won't mutate the original objects:






              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              console.log(
              result.map(({ data, ...rest }) => ({
              data: data.match(/^[^(]+[^( ]/)[0],
              ...rest
              }))
              );





              Note that if you make the final matched character in the regex [^( ] (match anything but a ( or a space), there's no need for trim anymore.






              share|improve this answer













              You need to access the data property of each object while you're iterating, in order to use a .match on it:






              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              for (let i = 0; i <= result.length - 1; i++) {
              const { data } = result[i];
              let regex = /^[^(]+/;
              let value = data.match(regex)[0].trim();
              result[i].data = value;
              }
              console.log(result);





              But, seeing as you're using react, you might consider using .map instead, which won't mutate the original objects:






              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              console.log(
              result.map(({ data, ...rest }) => ({
              data: data.match(/^[^(]+[^( ]/)[0],
              ...rest
              }))
              );





              Note that if you make the final matched character in the regex [^( ] (match anything but a ( or a space), there's no need for trim anymore.






              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              for (let i = 0; i <= result.length - 1; i++) {
              const { data } = result[i];
              let regex = /^[^(]+/;
              let value = data.match(regex)[0].trim();
              result[i].data = value;
              }
              console.log(result);





              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              for (let i = 0; i <= result.length - 1; i++) {
              const { data } = result[i];
              let regex = /^[^(]+/;
              let value = data.match(regex)[0].trim();
              result[i].data = value;
              }
              console.log(result);





              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              console.log(
              result.map(({ data, ...rest }) => ({
              data: data.match(/^[^(]+[^( ]/)[0],
              ...rest
              }))
              );





              let result = [{
              Id: 'a',
              temp: 5,
              data: 'abc (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'pqr (c:1'
              }, {
              Id: 'a',
              temp: 5,
              data: 'xyz (c:1'
              }];
              console.log(
              result.map(({ data, ...rest }) => ({
              data: data.match(/^[^(]+[^( ]/)[0],
              ...rest
              }))
              );






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 14 '18 at 6:21









              CertainPerformanceCertainPerformance

              83.3k144168




              83.3k144168























                  0














                  You can use split('(') instead of regex to get the same output:






                  let result = [{
                  Id: 'a',
                  temp: 5,
                  data: 'abc (c:1'
                  }, {
                  Id: 'a',
                  temp: 5,
                  data: 'pqr (c:1'
                  }, {
                  Id: 'a',
                  temp: 5,
                  data: 'xyz (c:1'
                  }];
                  for (let i = 0; i <= result.length - 1; i++) {
                  let value = result[i].data.split('(')[0].trim();
                  result[i].data = value
                  }
                  console.log(result);








                  share|improve this answer




























                    0














                    You can use split('(') instead of regex to get the same output:






                    let result = [{
                    Id: 'a',
                    temp: 5,
                    data: 'abc (c:1'
                    }, {
                    Id: 'a',
                    temp: 5,
                    data: 'pqr (c:1'
                    }, {
                    Id: 'a',
                    temp: 5,
                    data: 'xyz (c:1'
                    }];
                    for (let i = 0; i <= result.length - 1; i++) {
                    let value = result[i].data.split('(')[0].trim();
                    result[i].data = value
                    }
                    console.log(result);








                    share|improve this answer


























                      0












                      0








                      0







                      You can use split('(') instead of regex to get the same output:






                      let result = [{
                      Id: 'a',
                      temp: 5,
                      data: 'abc (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'pqr (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'xyz (c:1'
                      }];
                      for (let i = 0; i <= result.length - 1; i++) {
                      let value = result[i].data.split('(')[0].trim();
                      result[i].data = value
                      }
                      console.log(result);








                      share|improve this answer













                      You can use split('(') instead of regex to get the same output:






                      let result = [{
                      Id: 'a',
                      temp: 5,
                      data: 'abc (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'pqr (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'xyz (c:1'
                      }];
                      for (let i = 0; i <= result.length - 1; i++) {
                      let value = result[i].data.split('(')[0].trim();
                      result[i].data = value
                      }
                      console.log(result);








                      let result = [{
                      Id: 'a',
                      temp: 5,
                      data: 'abc (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'pqr (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'xyz (c:1'
                      }];
                      for (let i = 0; i <= result.length - 1; i++) {
                      let value = result[i].data.split('(')[0].trim();
                      result[i].data = value
                      }
                      console.log(result);





                      let result = [{
                      Id: 'a',
                      temp: 5,
                      data: 'abc (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'pqr (c:1'
                      }, {
                      Id: 'a',
                      temp: 5,
                      data: 'xyz (c:1'
                      }];
                      for (let i = 0; i <= result.length - 1; i++) {
                      let value = result[i].data.split('(')[0].trim();
                      result[i].data = value
                      }
                      console.log(result);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 6:18









                      Ankit AgarwalAnkit Agarwal

                      23.8k52144




                      23.8k52144























                          0














                          Try this one.



                            result.map(el => {
                          el.data = el.data.match(/^(w+)/)[0];
                          return el;
                          })





                          share|improve this answer




























                            0














                            Try this one.



                              result.map(el => {
                            el.data = el.data.match(/^(w+)/)[0];
                            return el;
                            })





                            share|improve this answer


























                              0












                              0








                              0







                              Try this one.



                                result.map(el => {
                              el.data = el.data.match(/^(w+)/)[0];
                              return el;
                              })





                              share|improve this answer













                              Try this one.



                                result.map(el => {
                              el.data = el.data.match(/^(w+)/)[0];
                              return el;
                              })






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 6:23









                              Nikita R.Nikita R.

                              1456




                              1456























                                  0














                                  let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]

                                  const new_result = result.map((item) => {
                                  let regex = /^[^(]+/;
                                  let value = item.data.match(regex)[0].trim();
                                  item.data = value;
                                  return item;
                                  });

                                  console.log(new_result);





                                  share|improve this answer




























                                    0














                                    let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]

                                    const new_result = result.map((item) => {
                                    let regex = /^[^(]+/;
                                    let value = item.data.match(regex)[0].trim();
                                    item.data = value;
                                    return item;
                                    });

                                    console.log(new_result);





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]

                                      const new_result = result.map((item) => {
                                      let regex = /^[^(]+/;
                                      let value = item.data.match(regex)[0].trim();
                                      item.data = value;
                                      return item;
                                      });

                                      console.log(new_result);





                                      share|improve this answer













                                      let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]

                                      const new_result = result.map((item) => {
                                      let regex = /^[^(]+/;
                                      let value = item.data.match(regex)[0].trim();
                                      item.data = value;
                                      return item;
                                      });

                                      console.log(new_result);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '18 at 6:26









                                      BrandonBrandon

                                      111




                                      111






























                                          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%2f53294158%2fhow-to-replace-a-value-of-a-key-in-a-object%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

                                          Xamarin.iOS Cant Deploy on Iphone

                                          Glorious Revolution

                                          Dulmage-Mendelsohn matrix decomposition in Python