This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
JavaScript console prints assigned value of variable before it has been assigned?
(8 answers)
Closed 9 years ago.
I've got a curious situation where I am trying to update an array of objects with a new object, but when I place a console.log statement before the push, it shows that the array already has the new object inside of it. Here is the basics of the code:
var array1=[{
"Name": "Lake",
"ID": "1234"
}];
var object1={
"Name": "Mountain",
"ID": "1234"
};
function testArray() {
console.log(array1);
array1.push(object1);
}
I'd eventually like to update the original array with new information if the object contains the same ID. If it does not contain the same ID, then it should be appended. This would happen with an $.each loop over array1.
I'd greatly appreciate any help. Thank you.
It is because you are doing this in a webkit broswer like Chrome and console.log() is being queued up (it's a webkit bug, it won't happen if you do it in Firefox or non-webkit broswer), and therefore it prints a later value of the array. You need to use
JSON.stringify(array1);
for a more accurate result.
If you want to update the original array with new information only when the object contains the same ID, use an if statement to check the ID:
function updateA(obj){
if(obj.ID === array1.ID){
array1.push(obj);
console.log( JSON.stringify(array1));
}
}
updateA(object1);
Related
This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Object comparison in JavaScript [duplicate]
(10 answers)
How to determine equality for two JavaScript objects?
(82 answers)
Closed 9 months ago.
I'm trying to use arr.filter() to find the missing element between two arrays, this has been answered plenty of times and i've seen threads like this one Finding missing element in two array for javascript that explain it actually very well. For some reason thought, i cant seem to get this to work.
This is my code
var x = [{Number:1},{Number:2},{Number:3}]
var y = [{Number:1},{Number:2}]
function getDifference(x,y){
x.filter(function(object,index,arr){
console.log(object,index,arr)
if(!y.includes(object)){
// print object
console.log(object) // <-- Prints all 3, should just print the missing one.
}
})
}
getDifference(x,y)
Basically, it just needs to print out the missing object. Which in this case, is {Number:3}
Instead, it prints every object.
I've tried with the code in the thread that i linked above, and still was having trouble. Not sure what i'm not understanding with it.
This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 1 year ago.
I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
strong text
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Chrome DevTools - inconsistent array length
(1 answer)
Closed 4 years ago.
When i see my book object in console it shows array but when i try to iterate it does not iterartes in loop.
console.log(book);// logs array of length 6
console.log(typeof book);// logs -> object
console.log(book.length);// logs 0
//this for loop does not iterates.
for(let i of book)
{
//logic
}
Please help me how to iterate?
my book object looks like below on console.
let i of book is wrong. Since it is an array of object, try to do below.
book.forEach( function (bookItem)
{
Your logic
});
This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));
This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Closed 5 years ago.
Why chrome developer tool showing 0 array length (first line Array[0]) even if there are total 9 objects in it?
At first line of image should be like Array[9] why it is showing Array[0]
In second image array has 13 objects so it is showing Array[13] not Array[0]
It seems like you are logging the output of the array before you are adding objects to your array. Something like
var arr = [];
console.log(arr);
arr.push({}); //pushing blank obj
Here, executing this code will result in Array[0] but it does hold your object and will return a length of 1 if you try arr.length.
This might also happen if you are having some sort of Async function which pushes item to your array, which will result in the same thing. For example,
var a = [];
setTimeout(function(){
a.push({});
a.push({});
}, 1000);
console.log(a);
I think, this behavior is intentional, where Chrome wants to show that initially, your Array was empty, later on, items were pushed on runtime.