.splice is not a function - angularjs

I have this filter, and I have error filtered[i].splice is not a function
.filter('tsFilter', function() {
return function(model, filter) {
var filtered = [];
if (model != null) {
for (var i = 0; i < model.length; i++) {
filtered[i] = model[i];
}
}
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered[i].splice(i, 1);
}
return filtered;
};
})
Why I have this error? how to solve that?

Here you have to update
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1); //updated
}
Hope this will help you out.

It looks like you want to use:
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1);
}
Although it is not a good idea to remove elements from the array that you are iterating over.

Related

Is their a way to find the closest number to 0 in an Array?

I've got an array like that
var tideArray = new Array();
tideArray.push({tide:"haute1", difference: "-14"});
tideArray.push({tide:"haute2", difference: "-3"});
tideArray.push({tide:"basse1", difference: "-9"});
tideArray.push({tide:"basse2", difference: "4"});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
For now, it's choosing the minimum number (-14) but I'd like to choose the closest number to 0.
Is there a way to do that ?
EDIT
I've tried that :
trace(closestToZero(tideArray));
function closestToZero(a:Array):int
{
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:int = 1; i < a.length; i++){
var thisDelta:int = Math.abs(0 - a[i].difference);
if(thisDelta < curDelta){
curIndex = i;
}
}
return curIndex;
}
But it seems that there is a mistake somewhere because the trace result is 3 (so it means that it's telling me that "basse2" (4) is the closest to 0... But, as you can see, it's "haute2" (-3) the closest).
I think it would be more efficient to simply loop over the array to find the item with the (absolute) minimum difference value:
if (tideArray.length > 0)
{
var minItem: Object = tideArray[0];
for (var index:int = 1; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
}
}
trace(minItem.tide);
}
Something like this
var tideArray = new Array();
...
function sortMyArray(a,b):int {
if (Math.abs(a) < Math.abs(b)) {
return -1;
}
if (Math.abs(a) > Math.abs(b)) {
return 1;
}
return 0;
}
tideArray.sort(sortMyArray);
Edit :
For your array.
function sortMyArray(a,b):int {
if (Math.abs(a.difference) < Math.abs(b.difference)) {
return -1;
}
if (Math.abs(a.difference) > Math.abs(b.difference)) {
return 1;
}
return 0;
}

Group an array with same value in actionscript

I have an array:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
I want to array:
var resultArr:Array = [5,6,7,8,9,10,11,12,13,14];
This may use full to you.
var a:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
a.sort();
var i:int = 0;
while(i < a.length) {
while(i < a.length+1 && a[i] == a[i+1]) {
a.splice(i, 1);
}
i++;
}
for other, see here
Try this:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
function group(subject:Array):Array
{
var base:Array = subject.slice().sort(Array.NUMERIC);
var prev:Number = base[0];
for(var i:int = 1; i < base.length; i++)
{
if(base[i] === prev)
{
base.splice(i, 1);
i--;
}
prev = base[i];
}
return base;
}
trace( group(exArr) );

Dojo function not working on IE8

I have a piece of code to select/deselect all Dojo checkboxes using a single master checkbox (which acts as a toggle) that works fine on firefox but doesn't on IE8. I tried hard to find the issue but was clueless, can anybody help. Im attaching the code below:
<script type="text/javascript">
dojo.require("dojo.parser");
dojo.ready(function() {
var checkboxes = [];
dojo.query('#waferCheck_cb input[type=checkbox]').forEach(function(node, index, arr){
checkboxes.push(arr[index].id);
var handle = dojo.connect(dijit.byId(arr[index].id), "onchange", function(evt){
var cbClicked = evt.target.id;
var cbStatus = dijit.byId(cbClicked).get("checked");
setCBSelection(checkboxes,cbClicked,cbStatus );
dojo.disconnect(handle);
});
});
});
function setCBSelection(checkboxes,cb_Clicked, cb_Status) {
var len = checkboxes.length;
if(len > 0) {
// get index of the checkbox clicked
var cb_pos = 0;
for(var i = 0; i < len; i++) {
if(cb_Clicked == checkboxes[i]) {
cb_pos = i;
break;
}
}
// If Select All checkbox clicked, set the other checboxes accordingly
if(cb_pos == 0) {
for(var i = 1; i < len; i++) {
dijit.byId(checkboxes[i]).set("checked", cb_Status);
}
} else {
// If any other checkbox is clicked, set the Select All accordingly
var allCBSameStatus = true;
for(var i = 1; i < len; i++) {
var curCBStatus = dijit.byId(checkboxes[i]).get("checked");
if(curCBStatus != cb_Status) {
allCBSameStatus = false;
break;
};
}
if(allCBSameStatus){
dijit.byId(checkboxes[0]).set("checked", cb_Status)
}else{
if(cb_Status == false) {
dijit.byId(checkboxes[0]).set("checked", cb_Status)
}
}
}
}
}
</script>
I believe IE8 doesn't like missing semicolons. I see 2 lines that are missing semicolons.
dijit.byId(checkboxes[0]).set("checked", cb_Status)
Have you tried to define function setCBSelection(...) before dojo.ready(..) (see below)? Maybe this helps as it is used before it was defined? If that doesn't help, it may be useful if you post some kind of error-message IE might throw...
dojo.require("dojo.parser");
function setCBSelection(checkboxes,cb_Clicked, cb_Status) {
var len = checkboxes.length;
if(len > 0) {
// get index of the checkbox clicked
var cb_pos = 0;
for(var i = 0; i < len; i++) {
if(cb_Clicked == checkboxes[i]) {
cb_pos = i;
break;
}
}
// If Select All checkbox clicked, set the other checboxes accordingly
if(cb_pos === 0) {
for(i = 1; i < len; i++) {
dijit.byId(checkboxes[i]).set("checked", cb_Status);
}
} else {
// If any other checkbox is clicked, set the Select All accordingly
var allCBSameStatus = true;
for(i = 1; i < len; i++) {
var curCBStatus = dijit.byId(checkboxes[i]).get("checked");
if(curCBStatus != cb_Status) {
allCBSameStatus = false;
break;
}
}
if(allCBSameStatus){
dijit.byId(checkboxes[0]).set("checked", cb_Status);
}else{
if(cb_Status === false) {
dijit.byId(checkboxes[0]).set("checked", cb_Status);
}
}
}
}
}
dojo.ready(function() {
var checkboxes = [];
dojo.query('#waferCheck_cb input[type=checkbox]').forEach(function(node, index, arr){
checkboxes.push(arr[index].id);
var handle = dojo.connect(dijit.byId(arr[index].id), "onchange", function(evt){
var cbClicked = evt.target.id;
var cbStatus = dijit.byId(cbClicked).get("checked");
setCBSelection(checkboxes,cbClicked,cbStatus );
dojo.disconnect(handle);
});
});
});
Btw: you should always use === to compare with 0 or false... You can use jsFiddle to let JSLint check your code which detacts many issues one doesn't see in the first place. Hope i could help you.

AS3 Array object showing up by ordering

I have 3 MC on stage which are all alpha=0
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 0;
}
I have one button which once I click then it will make 1 of the MC become alpha=1
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 1;
}
}
But with the script above it will makes all 3 MC become alpha=1.
I know that this can achieve by using below code:
if(mc1.alpha!=1){
mc1.alpha=1
}
if(mc2.alpha!=1){
mc2.alpha=1
}
if(mc3.alpha!=1){
mc3.alpha=1
}
this code will give what I want to achieve but if there is more than 3 MC the lines of script will be longer.
revealBtn.buttonMode = true;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
var mcArray:Array = [mc0,mc1,mc2];
function revealClick(event:MouseEvent):void
{
for(var i:uint = 0; i<mcArray.length; i++){
if(this['mc'+i].alpha !== 1){
this['mc'+i].alpha = 1;
break;
}
}
}
No that if statement would still turn all three to alpha 1.
Which of the three do you want to set to alpha = 1 when the click is made?
You could use something like this to set alpha = 1 on the first mc in the array which does NOT have alpha = 1
function revealClick(event:MouseEvent):void {
for (var j:int = 0; j < mcArray.length; j++){
if ( mcArray[j].alpha != 1 ){
mcArray[j].alpha = 1;
return;
}
}
}
Just use a counter.
var cnt : int = -1;
function revealClick(event:MouseEvent):void
{
if(++cnt < mcArray.length) mcArray[cnt].alpha = 1;
}

Using this to compare objects (Actionscript 3)

I am trying to find the position of the caller of an Event inside the _ar_cards array, however this piece of code won't work. What is wrong?
for( var i = 0; i < 3; i++ )
{
if(this == _ar_cards[i])
{
mouseEvent.target.alpha = 0.1;
}
}
Just add trace functions in the cycle:
for (var i:int = 0; i < 3; ++i)
{
trace(this.name, _ar_cards[i].name);
if (this == _ar_cards[i])
{
trace("It works!");
mouseEvent.target.alpha = 0.1;
}
}
And you will see where is the problem.

Resources