Javascript array length in click-function [duplicate] - javascript

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 8 years ago.
I'm having a hard time understanding this code. Could someone try to explain why an array can have elements and 0 length?
var myArray = [];
myArray["hello"] = 4;
myArray["world"] = 65;
$('#btn').on('click',function() {
console.log(myArray.length); // Prints 0
console.log(myArray); // Prints [hello: 4, world: 65]
console.log(myArray.length); // Prints 0
}

The .length property only pertains to numerically-indexed properties.

Related

Why After pushing a value into an array nested in an object. It shows the number of items in the array but the values are undefined? [duplicate]

This question already has answers here:
Why does Array.prototype.push return the new length instead of something more useful?
(6 answers)
Array.push return pushed value?
(7 answers)
Closed 2 years ago.
Iam a beginner and just starting with JS. Please forgive me if this is a stupid question.
After pushing a value into an array nested in an object. It shows the number of items in the array but the values are undefined rather than displaying the new array when i console log.
const warrior = {
health: 100,
equipment: ["sword", "sheild"],
location: {
x: 0,
y: 0,
},
walk: function(a, b) {
warrior.location.x = warrior.location.x + a;
warrior.location.y = warrior.location.y + b
},
strike: function(c) {
warrior.health = warrior.health - c
},
pickMeUp: function(equip) {
warrior.equipment = warrior.equipment.push(equip)
}
}
warrior.pickMeUp("axe");
console.log(warrior.equipment);
//undefined
//3
Im not sure why this is happening? Does this have anything to do with the way i wrote the function? writing it as >>
pickMeUp:function(equip){
this.equipment.push(equip)
removes - undefined
but, it still only returns array length and not the values.

Product prices array is not giving expected result while sorting [duplicate]

This question already has answers here:
How to sort an array of integers correctly
(32 answers)
Closed 3 years ago.
Product prices array is not giving me an expected result.
let productPrices = [10.33, 2.55, 1.06, 5.77];
console.log(productPrices.sort());
Result of above code:
(4) [1.06, 10.33, 2.55, 5.77]
Expecting:
(4) [1.06, 2.55, 5.77, 10.33]
You also have to make a function for comparing.
let productPrices = [10.33, 2.55, 1.06, 5.77];
console.log(productPrices.sort((a,b)=>a-b));

Deep clone object in Javascript except Object.assign [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
How to Deep clone in javascript
(25 answers)
Closed 3 years ago.
Object.assign doesn't deep copy an object. considering following code it should print 0 0 0 1 but it is printing 0 0 1 1.
var obj = {
"value": {
"default": 0
}
};
var newo = Object.assign({}, obj);
console.log(obj.value.default);
console.log(newo.value.default);
newo.value.default = 1;
console.log(obj.value.default);
console.log(newo.value.default);
I know we can use JSON.parse(JSON.stringify(obj)). but is that a best practice solution?

How can I check same elements in Array? [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 4 years ago.
I have an array, and I want an output if it contains more than 1 of the same element.
Example:
my_array = [1, 2, 3, 1];
if you want a Boolean output if an element is repeated you can do this:
var arr=[1,1,3,4]
let isDup=false;
arr.map(x=>(arr.indexOf(x)!==arr.lastIndexOf(x))?isDup=true:isDup)
console.log(isDup)
Convert the array to a Set. A Set can only contain unique values. If the Set's size is less than the array's length, there are duplicates:
const hasDuplicates = (arr) => arr.length > new Set(arr).size;
console.log(hasDuplicates([1, 2, 3])); // false
console.log(hasDuplicates([1, 2, 3, 1])); // true

JS: How to count all values in nested array [duplicate]

This question already has answers here:
Sum all integers in a multidimensional array javascript
(9 answers)
Closed 5 years ago.
I'm using a nested array like this:
const data = [
[0],
[2],
[[1], 3]
1
]
Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1).
And is it also possible to count how many arrays are used? This would be 5 arrays
const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);
This uses a recursive approach with reduce.

Categories