in my object
let obj = {
name:["Harry","Scren","Docis","Altab"],
age:[32,44,22,55]
}
I need to write down a self executing anonymous function in object that, when i call any member of the object it will execute that self executing anonymous function and it will check whether the length of both array (name and age) are equal or not, If not equal then throw an error .....I need to have something like
let obj = {
name:["Harry","Scren","Docis","Altab"],
age:[32,44,22,55],
(function(){
if(name.length != age.length){
throw new Error('both name and age 's length are not equal')
}
}()) // But this is not possible to create in object I have just showed down what I wanted to create
}
How to create something like that in javascript object ??
I'm not sure, but I think that's not possible...
But let me ask a question: why you have an obj containing two arrays? Hasn't more sense having an array of the obj which each element of the array contains one name and one age? I'll show you
const people = [
{ name: "Pearson 1", age: 20 },
{ name: "Pearson 2", age: 20 },
{ name: "Pearson 3", age: 20 },
{ name: "Pearson 4", age: 20 },
]
So you do not have to check the length since every obj has its own name and age field.
Related
So I have a series of 4 JSON objects with nested data inside each of them. Each of these objects are stored in an array called classes. Here is an example of how one of the class objects is formatted:
let class_A = {
professor: "Joey Smith",
numberStudents: 25,
courseCode: "COMS 2360",
seating: {
"FirstRow": {
0: {
firstName: "Sarah",
collegeMajor: "English",
},
1: {
firstName: "Bob",
collegeMajor: "Computer Engineering",
},
2: {
firstName: "Dylan",
collegeMajor: "Mathematics",
}
},
"SecondRow": {
3: {
firstName: "Molly",
collegeMajor: "Music"
}
}
}
};
I'm struggling to figure out how to access the very last fields within each class object (firstName and collegeMajor). The furthest I was able to get was the indexes beneath each row number.
let classes = [class_A, class_B, class_C, class_D];
let classesAvailable = document.getElementById('classes');
let class = classes[classesAvailable.value];
for(rowNum in class.seating){
for(index in class.seating[rowNum]){
console.log(index);
//console.log(class.seating[rowNum[index]].firstName);
}
}
So in this example, console.log(index) prints out:
0
1
2
3
but I'm unable to print the first name and college major of each student in each row. I was trying to follow a similar logic and do console.log(class.seating[rowNum[index]].firstName) but I get the error:
Cannot read properties of undefined (reading 'firstName')
Was wondering if anyone knows what's wrong with my logic here?
console.log(class.seating[rowNum][index])
This question already has answers here:
JavaScript - cannot set property of undefined
(8 answers)
Closed 1 year ago.
I have a JavaScript object customer that looks like this...
Object {
name: "John Doe",
age: "26"
}
I want to create a cars section and add some specifics so it looks like this...
Object {
name: "John Doe",
age: "26"
cars: {
car1 {
color: red,
type: ford
},
car2 {
color: blue,
type: GMC
},
car3 {
color: white,
type: toyota
}
}
}
I have tried to do this like this...
customer['cars']['car1'] = { color:'white', type:'toyota' }
But this gives me the error...
customer.cars is undefined
Where am I going wrong?
Problem
The error msg means that customer.cars has not been declared/assigned.
Solution
There are 2 options:
initialize empty cars object (In case that you want to assign each car)
var customer = {name:"John Doe",age:"26"};
customer.cars = {}; // init empty cars object
customer.cars.car1 = { color:'white', type:'toyota' };
customer.cars.car2 = { color:'blue', type:'GMC' };
console.log(customer);
assign all cars property directly.
var customer = {name:"John Doe",age:"26"};
customer.cars = {car1:{color:"red",type:"ford"},car2:{color:"blue",type:"GMC"},car3:{color:"white",type:"toyota"}};
console.log(customer);
You calling an undefined field cars on a customer record.
Try to define it first:
customer.cars = {}
And only then:
customer.cars.car1 = {...}
You can use [] syntax too:
customer['cars'] = {}
const obj = {
name: "John Doe",
age: "26"
}
obj.cars = {car1 : {color: 'white'}}
console.log(obj)
The way you're inserting the object is incorrect. See my example for the correct way to do this.
I have noticed that invoking .map() without assigning it to a variable makes it return the whole array instead of only the changed properties:
const employees = [{
name: "John Doe",
age: 41,
occupation: "NYPD",
killCount: 32,
},
{
name: "Sarah Smith",
age: 26,
occupation: "LAPD",
killCount: 12,
},
{
name: "Robert Downey Jr.",
age: 48,
occupation: "Iron Man",
killCount: 653,
},
]
const workers = employees.concat();
workers.map(employee =>
employee.occupation == "Iron Man" ? employee.occupation = "Philantropist" : employee.occupation
);
console.log(employees);
But considering that .concat() created a copy of the original array and assigned it into workers, why does employees get mutated as well?
This is happening because your objects within the array are still being referenced by same pointers. (your array is still referring to the same objects in memory). Also, Array.prototype.map() always returns an array and it's result should be assigned to a variable as it doesn't do in-place mapping. As you are changing the object's properties within your map method, you should consider using .forEach() instead, to modify the properties of the object within the copied employees array. To make a copy of your employees array you can use the following:
const workers = JSON.parse(JSON.stringify(employees));
See example below:
const employees = [
{
name: "John Doe",
age: 41,
occupation: "NYPD",
killCount: 32,
},
{
name: "Sarah Smith",
age: 26,
occupation: "LAPD",
killCount: 12,
},
{
name: "Robert Downey Jr.",
age: 48,
occupation: "Iron Man",
killCount: 653,
},
]
const workers = JSON.parse(JSON.stringify(employees));
workers.forEach(emp => {
if(emp.occupation == "Iron Man") emp.occupation = "Philantropist";
});
console.log("--Employees--")
console.log(employees);
console.log("\n--Workers--");
console.log(workers);
Note: If your data has any methods within it you need to use another method to deep copy
Problem analysis
workers = workers.map(employee =>
employee.occupation == "Iron Man" ? (employee.occupation = "Philantropist", employee) : (employee.occupation, employee)
);
[...] why does employees get mutated as well?
array.map() calls the passed function with each element from the array and returns a new array containing values returned by that function.
Your function just returns the result of the expression
element.baz = condition ? foo : bar;
which, depending on the condition, will
evaluate to foo or bar
assign that result to baz and
return that result
Further (expression1, expression2) will evaluate both expressions and return expression2 (see the comma operator).
So, although you return employee in both cases, you modify the original object with the left expression (expression 1).
Possible solution
You might want to create a new object using Object.assign()
array.map((employee) => Object.assign({ }, employee))
instead of using that array.concat() "trick". Using that mapping, you not only create a new array but also new objects with their attributes copied. Though this would not "deep copy" nested objects like { foo: { ... } } - the object accessible via the property foo would still be the same reference as the original. In such cases you might want to take a look on deep copying modules mentioned in the other answers.
The array references change but the copied array still reference the original objects in the original array. So any change in the objects in the array are reflected across all copies of the array. Unless you do a deep copy of the array, there is a chance that the some changes in the inner objects get reflected across each copy
What is the difference between a deep copy and a shallow copy?
Deep copies can be made in several ways. This post discusses specifically that: What is the most efficient way to deep clone an object in JavaScript?
map builds up a new array from the values returned from the callback, which caj easily be used to clone the objects in the array:
const workers = employees.map(employee => ({
...employee, // take everything from employee
occupation: employee.ocupation == "Iron Man" ? "Philantropist" : employee.occupation
}));
Or you could deep clone the array, then mutate it with a simple for:
const workers = JSON.parse(JSON.stringify(workers));
for(const worker of workers)
if(worker.ocupation == "Iron Man")
worker.ocupation = "Philantropist";
This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 4 years ago.
So I have a variable called elements that hold a bunch of data. Let's say, for example, that this is it:
{
name: "Jeff",
age: "18",
carList: [
"car1": "...",
"car2": "..."
],
surname: "Matt"
}
I also have an empty array, at the start, declared this way:
public myArray: Model[] = [];
Model is just a class that contains the definition of the elements.
What I'm trying to achive is to have a copy of elements without keeping any reference to the original variable. but I'm missing something. Here's a pseudo-code that do the trick (almost) :
myfunction(){
//Do stuff that change `car1` from `...` to `hello`
myArray.push(Object.assign({}, this.elements));
}
And finnally a I have a function that print everything in the myArray variable:
function print(){
this.myArray.forEach(el => {
console.log(el);
});
}
The problem is that every element of the array has the latest update done by me.
So if myFunction was called several time doing something like:
Change "Jeff" to "Jack"
Change age "18" to "25"
Change surname to "Seth"
what I see is three log showing this data:
{
name: "Jeff",
age: "25",
carList: [
"car1": "...",
"car2": "..."
],
surname: "Seth"
}
My question is: Why even using Object.asign it still keeping the reference to it's original object? I should keep the history of each modification, instead of having just the last edit in both of three edits I've made.
Thank you.
Try the following:
JSON.parse(JSON.stringify(element));
It keeps no refrences, as you can observe in the snippet below:
let element = { name: "Jeff", age: "18", carList: [ {"car1": "x"}, {"car2": "y"} ], surname: "Matt" };
let result = JSON.parse(JSON.stringify(element));
result.name = "x";
console.log(result);
console.log("****element*****");
console.log(element);
const monsters = {
'1': {
name: 'godzilla',
age: 250000000
},
'2': {
Name: 'manticore',
age: 21
}
}
I learn JavaScript from Codecademy, What does this code mean?
Is this two dimensional array? If not, what is it?
The data structure you are showing in your code example is not an array at all, it is an object. Arrays are defined using square brackets ([]) and their keys (indices) are not explicitly declared but rather assigned automatically.
So if you wrote your code like this, for example, you would have an array containing objects:
const monsters = [
{
name: 'godzilla',
age: 250000000
},
{
name: 'manticore',
age: 21
}
]
…so you could access the values by their array index, like so.
monsters[0].name; // godzilla
monsters[1].name; // manticore