0

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'
}];
3
  • Do months also have to be in calendar order for your Wednesdays? Commented Jul 20, 2019 at 18:21
  • 1
    Note that your unshift() with no argument isn't doing what you think it should and pop() is taking last element off regardless of what day it is and changing your indexing which is where error comes in Commented Jul 20, 2019 at 18:22
  • No, I want to place all 'Wednesdays' in sequence from the first index in array. I don't interest in calendar order for 'Wednesday'. Commented Jul 20, 2019 at 18:25

2 Answers 2

1

Try a sort.

objeTest.dates.sort((entryA, entryB) => {
  return (entryB.day === 'Wednesday') - (entryA.day === 'Wednesday')
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer, but you use 'date array', I don't need it. I should use 'objeTest.dates' that here is important.
@huso Use whatever reference you want to that array... doesn't change the fact that a sort is (seemingly) what you want.
I don't know. Thank you for advise and solution :)
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.