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

array unshift
array shift
javascript array pop first
remove element from array javascript
array unshift php
add element to beginning of array javascript
array splice
array pop php

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')
});

Array.prototype.unshift(), The elements to add to the front of the arr . Return value. The new length property of the object upon which the method was called. Description. shift function. This function returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop. This function returns undef if the array is empty otherwise returns first element of the array.

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

Array.prototype.pop(), The following code creates the myFish array-like object containing four elements and a length parameter, then removes its last element and� The unshift method inserts the given values to the beginning of an array-like object. unshift is intentionally generic. This method can be called or applied to objects resembling arrays.

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;

JavaScript Array Management with Push(), Pop(), Shift() and Unshift , And when an array has only one element, the first and last elements are one in the same. The JavaScript Array object provides four very useful methods: push(), � I have a array in which I am pushing objects, I am using array.unshift(object) to add the object to beginning of the array.. I am retrieving element from the array using var object = array.pop() which is removing object from the end of the array.

How to pop and unshift elements from an array in object?, Try a sort. objeTest.dates.sort((entryA, entryB) => { return (entryB.day === ' Wednesday') - (entryA.day === 'Wednesday') });. JavaScript Arrays - Pop - Push - Shift - Unshift students will learn to add stunning design elements to really make web pages visually aesthetic. and manipulate both HTML and CSS elements

Push, Pop, Shift and Unshift Array Methods in , Push, Pop, Shift and Unshift Array Methods in JavaScript methods i.e. push(), pop(), shift() and unshift() which allow us to add and remove elements from beginning and end of an array. Comparing values using Object.is() in Javascript. Someone asked me today about Perl array operations and it turned out he didn't know that all basic array operations, such as push, pop, shift, unshift can be implemented with just splice. So I thought I'd write a blog post that shows how awesome splice is.

JavaScript Array unshift() Method, Definition and Usage. The unshift() method adds new items to the beginning of an array, and returns the new length. Note: This method changes the length of an � pop. The pop function will remove and return the last element of an array.. In this first example you can see how, given an array of 3 elements, the pop function removes the last element (the one with the highest index) and returns it.

Comments
  • Do months also have to be in calendar order for your Wednesdays?
  • 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
  • No, I want to place all 'Wednesdays' in sequence from the first index in array. I don't interest in calendar order for 'Wednesday'.
  • 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 :)