How to remove elements inside specified index of array using angular - javascript

I need to remove some elements inside an array with specified index. I am trying to use slice function for it but it is giving me undefined error.
This is my controller code:
if($scope.startColm<$scope.endColm){
//Suppose here startColm value is 1 and endColm value is 4
for(var j=0;j<rowCellData.length;j++){
//rowCellData is an array. Attaching its image for reference
for(var k=$scope.startColm;k<($scope.endColm-$scope.startColm);k++){
var cellData=rowCellData[j].slice($scope.startColm,$scope.endColm)
//Here i want to remove data inside rowCellData from specified index.
}
}
}
rowCellData consists data like this:
Inside each index there is some data which I need to fetch based on the specified startColm and endColm value. Like here i want the data from 1st to 4th position for all the index of rowCellData.
Please suggest how to remove data inside array elements.

You want the 'splice' method, not the 'slice' method. However, since you have objects inside the main array and not other arrays, you could use delete to remove the items:
for (var i = $scope.startColm; i <= $scope.endColm; i++) {
delete rowCellData[j][i]
}

Related

Adding a different value of the same variable to an array

Using my micro:bit I am trying to add the value of a variable called sendText to an array without overwriting its previous stored value for that variable.
input.onGesture(Gesture.Shake, () => {
list.push(sendText)
binSend = 0
basic.showString(sendText)
})
My array is called list
let list: string[] = []
I am trying to store single characters in an array then outputting them. If there is a better alternative to using an array I would gladly accept it.
To add a value to an array you use push function, after, if you need to group the characters pushed to array for output you could to use for your specific example list.join('')

Angularjs - delete searched object from json array

I'm using a ng-change directive to get an object that change. When i get it i have to check if exists in another array of objects, and if it does i have to delete from it.
what can i do? i tried with indexOf but it didn't work.
//#Param ObjectItem: change object from UI using ng-change
$scope.itemValue = function (ObjectItem) {
//collection of required items
var requiredItem = dataPublicationService.getRequired();
//check if item exist in collection. DIDN'T WORK!
if(requiredItem.indexOf(publi) !== -1){
//get found index
var idx = requiredItem.indexOf(publi);
//delete founded item.
requiredItem.splice(idx, 1);
}
};
What other solution can i implement?
The indexOf function doesn't delete from an array, it just finds the index of the given object if found. You might consider filtering the array and testing for a match, rather than relying on indexOf to find a matching object.
Once you find your record, you need to actually alter the array using something like Array#splice.

AngularJs pushing multiple values from array to another with databinding refreshing

I am getting an array from the server which can contain 0..n elements in an array. I then add that to array I use locally for databinding (basically cache data in client). When doing it this way databiding works without any problems:
for (var i = 0 ; i < data.Result.length ; i++) {
scope.cachedData.push(data.Result[i]);
}
Meaning - view refreshes, everything works. But when I try: scope.cachedData.concat(data.Result); it won't work. Why is that?
If you want to push everything in a single instruction use apply without breaking the reference to scope.cachedData
Array.prototype.push.apply(scope.cachedData, data.Result);
Also, I know this is a little bit off topic but if you want to insert at a specific index you can use splice with apply
// I definitely want to prepend to my array here
var insertionIndex = 0,
// we don't want to delete any elements here from insertionIndex
deleteCount = 0;
// Because we use apply the second argument is an array
// and because splice signature is (startIndex, noOfElementsToDelete, elementsToInsert)
// we need to build it
Array.prototype.splice.apply(scope.cachedData, [insertionIndex, deleteCount].concat(data.Result));
Imagine your array scope.cachedData = [3,4]; and data.Result = [1,2];, with the code above scope.cachedData will become [1,2,3,4].

Javascript removing and rearranging array elements

I have this collection of images resources where that is stored in array, the user will select an image and then the selected image will be removed from the list(also from the array) and after that The array would be rearrange. How could I perform such task? (as much as possible I do not want to use an open source library)
Sounds like you need to look up splice() method. It allows you to add and remove one to many items within an array at any index.
here's reference for it.
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice
your question lacks a code example but you can use Array.splice(index,number) whereas index is zero based and number is how many items to remove.
images.splice(selectedIndex,1);
Simply, you can create a temporary array where you store the initial array elements you need and reassign the value of your initial array to the temporary array.
function clean_array(my_array){
var no_need_value = 'value you want to remove'
var tmpArray = new Array()
for (var i = 0; i < my_array.length; i++)
if (my_array[i] != no_need_value)
tmpArray.push(my_array[i])
my_array = tmpeArray
}

best way to append an item to a list within a JSON object?

I'm working on JavaScript and I have this JSON object:
var obj ={"name" : "objName",
"dynamicList" :[]};
and then I add some items to my list like this:
obj.dynamicList["someStringID"] = {"watever"};
It's important that I use a string as indexer for each item on my list (i didn't know this could be done until recently).
My only problem now is that whenever I ask for obj.dynamicList.lenght I get 0, unles I manually set the proper number... I was wondering if there's a better way to add items to my list?
Thanks!!
In Javascript, string index is not really an index. It's actually an attribute of the array object. You could set and get the value with the string index, but it's actually an empty array with some attributes. Not only .length, but also .sort(), .splice(), and other array function would not work. If there is a need to use array functions, I would use number as an index to make it a real item in the array.
If you have to use the string as an index, you couldn't rely on .length function. If there is no need to support IE prior to version 9, the Object.keys as suggested by #strimp099 should work. or you may have to create function to count the number of attributes for example:
function len(obj) {
var attrCount = 0;
for(var k in obj) {
attrCount++;
}
return attrCount;
}
and then call
len(obj.dynamicList);
Use the following the find the length of dynamicList object:
Object.keys(obj.dynamicList).length
To do this "the right way," you will have to make obj.dynamicList an object instead of an array; use {} instead of [] to set the initial value.
How to efficiently count the number of keys/properties of an object in JavaScript?
dynamiclist is an object, the length is not the length property you expect from an array.

Categories

Resources