How to remove object from inside .each() [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove a property from an object?
I am comparing two JSON objects and then deleting the old items from a list with this code:
dangerousPeople = ({1:{title:"Jackie Chan", user:"Jackie"}, 2:{title:"Chuck Norris", user:"Chuck"}, 3:{title:"Britney spears", user:"Britney"}});
newDangerousPeople = ({1:{title:"Jackie Chan", user:"Jackie"}, 3:{title:"Britney spears", user:"Britney"}});
$.each(dangerousPeople, function(index)
{
if(!newDangerousPeople[index]){
$('#dangerousPeople #id'+index).slideUp("normal", function() { $(this).remove(); } );
delete dangerousPeople.index;
}
});
The part of the script that slidesup the element works, but deleting the element from the object I can't make it work.
I tried with delete dangerousPeople.index but doesn't work, also tried delete $(this) but no luck either.
So how should I remove the element from itself?

Try:
...
delete dangerousPeople[ index ];
...

Related

How to solve this javascript array delete method problem [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 3 years ago.
My code :
<html>
<body>
<p id='demo'></p>
<script>
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
delete array[2];
document.getElementById('demo').innerHTML = array;
</script>
</body>
</html>
When I running this code...
The output
Comma quotation were not deleted after execution.
Help me to solve this (or) Suggest any other ways for deleting values in the specified index in js array.
var a = ['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
a.splice(2,1);
console.log(a);
Change
delete array[2]
to
array.splice(2,1)
Regards.
I would say use:
array.splice(index, 1);
the "index" parameter is an index of the element you want to remove and "1" is a number of elements you want to remove. splice() will take care of the indexes of removed/added elements for you.
If you don't know the index of the element you can try:
array.indexOf("BMW");
Thanks.
var array=['BENZ','SKODA','BMW','MERCEDAS','VOLVO'];
array.pop[2];
By following you can delete the element in array .pop[]

How to choose two div elements from an array and execute a function on them? [duplicate]

This question already has answers here:
How do I select a sibling element using jQuery?
(11 answers)
Closed 3 years ago.
I have an array $t with HTML elements:
I need to execute a function on elements that have class row (2 elements).
Right now, I'm trying to filter elements with hasClass method but receive the following mistake:
hasClass(...).each is not a function
How can I iterate over the array and get elements with row class?
Here is my code:
var $t = $(this).siblings('div');
console.log($t);
$t.hasClass("row").each(function(){
...
})
UPDATE
I've changed a code a bit:
var _t = $(this).siblings('div.row');
console.log('t ' + _t);
_t.each(function( index, value ){
console.log('value ' + value);
var checkboxes = value.find('input[type="checkbox"]');
console.log(checkbox);
checkboxes.trigger( "click" );
})
Now, I'm receiving the following error:
value.find is not a function
According to jquery doc hasClass will return only boolean. As mentioned in comments you should use $('div.row') selector instead

JavaScript - cloneNode gives different result [duplicate]

This question already has answers here:
Javascript clone node is not copying all values from cloned to new object
(3 answers)
Closed 5 years ago.
I am trying to clone the form before submition in JavaScript using cloneNode. The original form has an answer for a selected value but not the cloned one. Following is the code.
encodeHTMLCollectionToBase64(document.forms['formen']).submit();
function encodeHTMLCollectionToBase64(form) {
encryptedForm = form.cloneNode(true)
Array.from(encryptedForm).forEach(function(item) {
if (item.childElementCount > 0) {
for(var i=0;i < item.childElementCount;i++) {
item[i].value = btoa(encodeURIComponent(item[i].value));
}
}
else {
item.value = btoa(encodeURIComponent(item.value));
}
});
encryptedForm.style.visibility = 'hidden';
document.body.appendChild(encryptedForm);
return encryptedForm ;
}
Upon inspection I found that the encryptedForm (cloned form) has empty value for one select element but it is there in form (original form). Why is that?
Am i doing something wrong here?
The form's selections are saved in the browser, not in the form's DOM elements, so when you clone it, the selections will not be copied over. It's possible to copy the selections over if you use JavaScript to manage the 'selected' prop on all of your form elements, or you could store the selections in a separate variable and reapply them later.

how to select a particular tag from seletected array of tags using Jquery [duplicate]

This question already has answers here:
Selecting the first "n" items with jQuery
(6 answers)
Closed 9 years ago.
I am selecting all tags where href attribute is starting with "picThumbImgA_" as below.
$('a[id^="picThumbImgA_"])')
now i want to update some attributes of first four tag. I can select first as
$('a[id^="picThumbImgA_"]):first')
and last as $('a[id^="picThumbImgA_"]):last')
odd as $('a[id^="picThumbImgA_"]):odd')
and even as $('a[id^="picThumbImgA_"]):even')
But how can I process first four tags only.
Please help me out for the same.
You can try the Zero-based index selector :lt(), like :
$('a[id^="picThumbImgA_"]):lt(4)')
From jQuery's documentation:
Select all elements at an index less than index within the matched set.
You could also use :nth-child(), like:
$('a[id^="picThumbImgA_"]):nth-child(-n+4)')
You can use the jQuery each() function and exit the function by return false when you have processed the first 4.
$('a[id^="picThumbImgA_"])').each(function(index, el) {
if(index < 4) {
//do something
} else {
return false;
}
});

Add multiple element using jquery each? [duplicate]

This question already has answers here:
Why do multiple `.appendTo` calls on a newly created jQuery element only append it once?
(2 answers)
Closed 3 months ago.
I'm new here and I'd like to ask a question about jQuery.
i have some problem with jquery each, and i have code :
$("#search-submit").click(function(){
var item = $("<span>asdf</span>");
$("body").append(item, item);
}
I just can't figure out, why is this simple code don't work. Instead of print "asdf" twice, i just got one "asdf" printed each time i click the button..
this also don't work. this is so silly...
$("body").append(item);
$("body").append(item);
Thanks a lot... and sorry about my bad english
Denny.
You can append a jQuery object only one time, you can clone the object:
$("#search-submit").click(function() {
var item = $("<span>asdf</span>");
$("body").append(item, item.clone());
})
Or append a string:
$("#search-submit").click(function() {
var item = "<span>asdf</span>";
$("body").append(item, item);
})
http://jsfiddle.net/yKyAL/
You can use clone
$("#search-submit").click(function() {
var item = $("<span>asdf</span>");
$("#test").append(item.clone(), item.clone());
});​
Check the fiddle here

Categories