skip to Main Content

How can i edit an array inside an object?
My array contains a string, but i wanted the string to be split up via .split("") or another method.
This is how it looks at the moment:

line1_x = [
{"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
{"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
{"id": 3, "topic_no": 453, "keywords": ["[Keyword1, Keyword2, Keyword3]"] }
]

In this case, the brackets are part of the string. I would remove them via .replaceAll() or regex, i guess?
What i would like to archieve is following:

line1_x = [
{"id": 1, "topic_no": 451, "keywords": ["Keyword1", "Keyword2", "Keyword3"] },
{"id": 2, "topic_no": 452, "keywords": ["Keyword1", "Keyword2", "Keyword3"] },
{"id": 3, "topic_no": 453, "keywords": ["Keyword1", "Keyword2", "Keyword3"] }
]

I assume i need to iterate through the line1_x via a normal loop and use the .split("") method?

4

Answers


  1. To edit the arrays inside the objects and split the strings into separate keywords, you can loop through the array and use the map function to update the keywords property. Here’s a step-by-step explanation of how you can achieve this in JavaScript:

    // Your original array of objects
    const line1_x = [
      {"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"]},
      {"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"]},
      {"id": 3, "topic_no": 453, "keywords": ["[Keyword1, Keyword2, Keyword3]"]}
    ];
    
    // Define a function to split the string and remove the square brackets
    function splitAndTrimKeywords(str) {
      return str.split(", ").map(keyword => keyword.replace("[", "").replace("]", "").trim());
    }
    
    // Loop through the array and update the 'keywords' property for each object
    const updatedLine1_x = line1_x.map(obj => {
      return {
        ...obj,
        keywords: splitAndTrimKeywords(obj.keywords[0]) // Assuming there's always only one string in the 'keywords' array
      };
    });
    
    // Now 'updatedLine1_x' contains the objects with split keywords
    console.log(updatedLine1_x);
    Login or Signup to reply.
  2. Here is a simple solution. Note that I came to the conclusion that your keywords will always have one member.

    const line1_x = [
      {"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 3, "topic_no": 453, "keywords": ["[Keyword1, Keyword2, Keyword3]"] }
    ];
    
    function formatKeywords(line) {
      return line.map(row => {
        row.keywords = row.keywords[0].replaceAll(/[[]']+/g, "").split(", ");
        return row;
      });
    }
    
    console.log(formatKeywords(line1_x));
    Login or Signup to reply.
  3. I propose a regex solution:

    b[^][,]+b
    

    Matching everything but not ,, [ or ] sitting between two word boundaries.

    const line1_x = [
      {"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 3, "topic_no": 453, "keywords": ["[Keyword1,Keyword2, Some Keyword3 ]"] }
    ];
    
    const output = line1_x.map(({ keywords, ...rest }) => ({ 
      ...rest,
      keywords: keywords[0].match(/b[^][,]+b/g)
    }));
    
    console.log(output);
    Login or Signup to reply.
  4. If your keywords consist of alphanumeric chars only you could use a simple regex /w+/g to match all keywords. Also note that using rest syntax involves iterators which are slow, so if the object structure is known and performance is important I suggest to compose object manually.

    Also it’s not clear whether you want to get a new array or modify the existing one.

    To create a new array (pure approach):

    const result = line1_x.map(({id, topic_no, keywords:[kw]}) => ({
      id, topic_no, keywords: kw.match(/w+/g)
    }));
    
    console.log(result);
    <script>
    const line1_x = [
    {"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
    {"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
    {"id": 3, "topic_no": 453, "keywords": ["[Keyword1, Keyword2, Keyword3]"] }
    ]
    </script>

    And the benchmark:

    enter image description here

    <script benchmark data-count="300000">
    const line1_x = [
      {"id": 1, "topic_no": 451, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 2, "topic_no": 452, "keywords": ["[Keyword1, Keyword2, Keyword3]"] },
      {"id": 3, "topic_no": 453, "keywords": ["[Keyword1, Keyword2, Keyword3]"] }
    ]
    
    // @benchmark Hao Wu
    line1_x.map(({ keywords, ...rest }) => ({ 
      ...rest,
      keywords: keywords[0].match(/b[^][,]+b/g)
    }));
    
    // @benchmark Thomas (in-place)
     line1_x.map(row => {
        row.keywords = row.keywords[0].replaceAll(/[[]']+/g, "").split(", ");
        return row;
      });
    
    // @benchmark Alexander
    line1_x.map(({id, topic_no, keywords:[kw]}) => ({
          id, topic_no, keywords: kw.match(/w+/g)
        }));
    
    // @benchmark Alexander (in-place)
    line1_x.forEach(item => item.keywords = item.keywords[0].match(/w+/g));
    line1_x;
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search