skip to Main Content

In this list of objects we have value groupID, Which contains a list.

What I want is to replace the first value in the list with the last value in the previous list.

let testArr = [
    {groupID: [1900, 1890, 1880], Letter: "A"},
    {groupID:[1888, 1898, 1908, 1918, 1928, 1938], Letter: "B"},
    {groupID: [1927, 1917, 1907], Letter: "A"},
    {groupID: [1912, 1922, 1932, 1942, 2012, 2022, 2032, 2042], Letter: "B"},
    {groupID: [2039, 2029, 2019, 2009], Letter: "A"},
    {groupID: [2013, 2023, 2033], Letter: "B"},
]

The result is:

let testArr = [
    {groupID: [1900, 1890, 1880], Letter: "A"},
    {groupID:[1880, 1898, 1908, 1918, 1928, 1938], Letter: "B"},
    {groupID: [1938, 1917, 1907], Letter: "A"},
    {groupID: [1907, 1922, 1932, 1942, 2012, 2022, 2032, 2042], Letter: "B"},
    {groupID: [2042, 2029, 2019, 2009], Letter: "A"},
    {groupID: [2009, 2023, 2033], Letter: "B"},
]

I have this code, but it’s silly and doesn’t work with async and await:

let add = []

for (let i = 0; i < testArr.length; i++){
    if (testArr[i].groupID.length != 0){
        add.push(testArr[i].groupID[testArr[i].groupID.length-1])
    }
}
for (let i = 1; i < testArr.length; i++){

    testArr[i].groupID.splice(0,1, add[i-1])
}

and Thanks in advance.

3

Answers


  1. I don’t understand why your code needs to work with async/ await but a possible solution to your problem could look as follows:

    const testArr = [
        {groupID: [1900, 1890, 1880], Letter: "A"},
        {groupID: [1888, 1898, 1908, 1918, 1928, 1938], Letter: "B"},
        {groupID: [1927, 1917, 1907], Letter: "A"},
        {groupID: [1912, 1922, 1932, 1942, 2012, 2022, 2032, 2042], Letter: "B"},
        {groupID: [2039, 2029, 2019, 2009], Letter: "A"},
        {groupID: [2013, 2023, 2033], Letter: "B"}
    ];
    
    let lastValue;
    const result = testArr.map((o) => {
      const tmpLastValue = o.groupID[o.groupID.length - 1];
      if (lastValue) {
         o.groupID = [lastValue, ...o.groupID.splice(1)];
      }
      lastValue = tmpLastValue;
      return o;
    });
    
    console.log(result);
    Login or Signup to reply.
  2. I am not sure if you’re trying to replace the elements in the same array of objects or need to return a new array. However, here is a solution to replace the elements in the same array.

    Update: Based on your comment, it seems you need this transformation to happen from the end to the beginning of the list. I have commented my previous code and kept it for reference, though.

    let testArr = [
        {groupID: [1900, 1890, 1880], Letter: "A"},
        {groupID:[1888, 1898, 1908, 1918, 1928, 1938], Letter: "B"},
        {groupID: [1927, 1917, 1907], Letter: "A"},
        {groupID: [1912, 1922, 1932, 1942, 2012, 2022, 2032, 2042], Letter: "B"},
        {groupID: [2039, 2029, 2019, 2009], Letter: "A"},
        {groupID: [2013, 2023, 2033], Letter: "B"},
    ]
    
    // testArr.forEach((item, index) => {
    //     index && (item.groupID[0] = testArr[index-1].groupID[testArr[index-1].groupID.length-1]);
    // })
    
    for (let i = testArr.length - 1; i > 0; i--) {
        testArr[i].groupID[0] = testArr[i-1].groupID[testArr[i-1].groupID.length-1];
    }
    
    console.log(testArr);

    Please note that there could be multiple solutions to this depending upon what exactly you need to achieve. Also, you need to explain what do you mean by running this code with async and await?

    Login or Signup to reply.
  3. The OP’s specification can be implemented almost literally …

    testArr
      // ... for each array item ...
      .forEach((item, idx, arr) => {
    
        // ... which is not the first item ...
        if (idx >= 1) {
    
          // ... replace the current item's first `groupID` array item ...
          item.groupID[0] =
            // ... with the previous items's last `groupID` array item.
            arr[idx - 1].groupID.at(-1);
        }
      });
    
    console.log({ testArr });
    .as-console-wrapper { min-height: 100%!important; top: 0; }
    <script>
      const testArr = [
        { groupID: [1900, 1890, 1880], Letter: "A" },
        { groupID: [1888, 1898, 1908, 1918, 1928, 1938], Letter: "B"} ,
        { groupID: [1927, 1917, 1907], Letter: "A" },
        { groupID: [1912, 1922, 1932, 1942, 2012, 2022, 2032, 2042], Letter: "B" },
        { groupID: [2039, 2029, 2019, 2009], Letter: "A" },
        { groupID: [2013, 2023, 2033], Letter: "B" },
      ];
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search