Arrays are very common datatype which we will be using in our Day-to-day life. In which inserting the records and retrieving based on
the index is very common scenario.
Push: – To Insert an item at the bottom of the array, we will use the Push method.
Un-Shift: – To Insert an item at the top of an array, we can use unshift method.
The example is as follows.
myParentArray.map(parentArrayItem => {
if (parentArrayItem.Action != null) {
childArray.push({
Id: parentArrayItem.Id,
Title: parentArrayItem.Title,
})
}
else {
childArray.unshift({
Id: parentArrayItem.Id,
Title: parentArrayItem.Title,
})
}
})
In the above example, if the parentArrayItem.Action has value, then it will be pushed at the bottom of the ChildArray. If there is no Action, then it will be placed on the top of the ChildArray.
Happy Coding
Sathish Nadarajan
Leave a comment