简体   繁体   中英

How to pop and unshift elements from an array in object?

I have an object. And I have a scenario that what do I want to do with it.

Scenario : All arrays and features were added in objeTest. After that I will call it, I want to remove 'Wednesday' value from default index. And I want to place all 'Wednesdays' in sequence from the first index.

*The following getted error :

Uncaught TypeError: Cannot read property 'day' of undefined*

The following is an example for my issue. You could try and will see error message from console.

I didn't find any solution, I need your advise and solution.

Code :

var objeTest = {
  name : 'objeTest',
  langs : {
          0 : 'EN',
          1 : 'VI',
          2 : 'RU',
          3 : 'AR'
          },
  commentGeneral : 'testComment'
};

    var date = [{
        day : 'Sunday',
        month : 'July',
        comment : ''
      },
      {
         day : 'Wednesday',
         month : 'June',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
},
{
         day : 'Wednesday',
         month : 'June',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
},
          {
         day : 'Friday',
         month : 'February',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
}];

/**
  *  I don't want to remove that using the array adding 
  *  in the object process ( objeTest.dates = date.filter(...)etc).
  */
objeTest.dates = date;  // You couldn't change from adding process. I don't need this solution from here.
// If you couldn't understand, please read again scenario. Thanks for your interesting.

var myObjLeng = objeTest.dates.length;
console.log(objeTest);
for(var i = 0; i < myObjLeng; i++) {
    if(objeTest.dates[i].day == 'Wednesday') {
        objeTest.dates[i].pop();
        objeTest.dates[i].unshift();
    }
};

console.log(objeTest);

So the new dates of the object I want to get should look like this in the object:

[
 {
         day : 'Wednesday',
         month : 'June',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
},
{
         day : 'Wednesday',
         month : 'August',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
},
    {
        day : 'Sunday',
        month : 'July',
        comment : ''
      },

          {
         day : 'Friday',
         month : 'February',
         comment : 'lorem ipsum dolor sit amet consectetur adipiscing elit'
}];

Try a sort .

objeTest.dates.sort((entryA, entryB) => {
  return (entryB.day === 'Wednesday') - (entryA.day === 'Wednesday')
});

You could use sort, please read this: Js Array Sort from w3schools

Example Codes:

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b}); // Output: 1,5,10,25,40,100
points.sort(function(a, b){return b - a}); // Output: 100,40,25,10,5,1

If you need to separate the array the easiest way is to forget about the original one and then set it again.

not_wednesdays = [];
wednesdays = [];

objeTest.dates.forEach(date=>{
    if (date.day=="Wednesday") {
        wednesdays.push(date);
    }
    else {
        not_wednesdays.push(date);
    }
});
objeTest.dates = not_wednesdays;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM