This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 5 years ago.
Im totally new in JS. Could someone help me please? I need to prepare some objects atributes which depends on other objects attributes
How can I achive that?
I tried that solution:
var Photo = {
current: {
coordinates: "2208,1922",
name: "current"
},
start: {
coordinates: '2408,1822',
name: 'start',
newCoordinates: Photo.current.coordinates
}
};
I'm getting an error:
module initialization error: TypeError
var current = {
coordinates: "2208,1922",
name: "current"
}
var Photo = {
current: current,
start: {
coordinates: '2408,1822',
name: 'start',
newCoordinates: current.coordinates
}
};
console.log(Photo);
You just need to wait for the object to be initialized, then you can grab that property. See this code:
var Photo = {
current: {
coordinates: "2208,1922",
name: "current"
},
start: {
coordinates: '2408,1822',
name: 'start'
}
};
Photo.start.newCoordinates = Photo.current.coordinates; // 2208,1922
Related
This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Array.includes() to find object in array [duplicate]
(8 answers)
Javascript: Using `.includes` to find if an array of objects contains a specific object
(7 answers)
Closed 7 months ago.
Trying to add new object value in an array but not working. How to add it? If anyone knows please help to find the solution.
getting this error:
Property 'includes' does not exist on type '{ name: string; id:
string; }[]'. Do you need to change your target library? Try changing
the 'lib' compiler option to 'es2016' or later.
app.component.ts:
public a = { name: 'test1', id: '12345' };
public b = { name: 'test2', id: '12345' };
addVala() {
if (this.arr.includes(this.a)) {
console.log('This obj already there');
} else {
this.arr.push(this.a);
}
console.log(this.arr);
}
Demo : https://stackblitz.com/edit/angular-ivy-jj7sna?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.ts
You can simplify your "add" logic by passing-in the object that you want to add and checking if another object (already in the list) shares the same ID.
const arr = [
{ name: 'test1', id: 'A' },
{ name: 'test3', id: 'C' },
];
const a = { name: 'test1', id: 'A' };
const b = { name: 'test2', id: 'B' };
const add = (obj) => {
if (!arr.find(({ id }) => id === obj.id)) {
arr.push(obj);
console.log(`Added ${obj.id}`);
} else {
console.log(`Object ${obj.id} already exists!`);
}
}
function addA() { add(a); }
function addB() { add(b); }
<button onclick="addA()">Add A</button>
<button onclick="addB()">Add B</button>
This question already has answers here:
How to convert array of items to array of objects?
(2 answers)
Closed 1 year ago.
I am trying to convert an array of elements to an array of objects in Javascript (react)
Here is the data I am getting from my API
"versions": [
"1.0.1.2",
"1.0.22.0",
"1.1.0.12",
"2.5.2.6",
"2.5.2.7",
"2.7.5.11",
"2.7.7.7",
"3.9.2.94",
"3.9.3",
"5.2.0.87",
"9.5.0.210" ]
And I am trying to convert to an array of object which should look like this
options = [
{ value: "1.0.1.2", label: "1.0.1.2" },
{ value: "1.0.22.0", label: "1.0.22.0" },
{ value: "2.5.2.6", label: "2.5.2.6" },
];
I tried using the map function
versions = VersionloginData.data.versions.map((version) => [version.value, version.label])
But didn't work out well , i am getting undefined as value objects
You needed to return an object inside the map callback:
versions = VersionloginData.data.versions.map((version) => ({ value: version, label: version }))
Its should be.
const data = {
"versions": [
"1.0.1.2",
"1.0.22.0",
"1.1.0.12",
"2.5.2.6",
"2.5.2.7",
"2.7.5.11",
"2.7.7.7",
"3.9.2.94",
"3.9.3",
"5.2.0.87",
"9.5.0.210"]
}
const output = data.versions.map(item => ({ value: item, label: item }));
console.log(output);
Why your code is not working?
You are accessing incorrect nodes with [version.value, version.label]. value and label doesnot exist on version. Instead, you should return an object with keys value and label having same value.
you can try this
var options = []
versions.forEach((v)=>{
options.push({
value: v,
label: v,
})
})
This question already has answers here:
How to set a Javascript object values dynamically?
(7 answers)
Closed 4 years ago.
I want to create a new object dynamically and insert into the inside of columns object
dynamicCreate = [
{
columns: {
title: {
title: "Title"
}
}
}
]
Create dynamically like
name: {
title: "Name"
},
and insert next of
title: {
title: "Title"
},
You can try using the dot notation
var obj={};
obj.title={};
obj.title.title="name";
console.log(obj)
Javascript is a dynamic language. so you can assign any props dynamically to the object.
var obj={
name:'foo'
};
obj.extraInfo={
bar:'baz'
}
console.log(obj);
This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
Closed 5 years ago.
I have an object in array like the following:
bears: [
{
Yogi: "123kg",
Pooh: "110kg",
Grizly: "112kg",
BooBoo: "200kg",
Polar: "100kg",
}
]
`
What is the best way to iterate through such object in order to display both names and values in the row, like returning something in the type of: <p>${name} ${value}</p>
So I would display:
Yogi 123kg
Pooh 110kg
Grizly 112kg
BooBoo 200kg
Polar 100kh
It's an array containing an object, not an object. Anyway just get the first item of the array.
This should work:
Object.keys(bears[0]).map(key => <p>{`${key} ${bears[0][key]}`}</p>);
I think that the JSON object's structure itself is wrong.
It should be structured like this:
var bears = [{
name: "Yogi",
weight: "123kg"
}, {
name: "Pooh",
weight: "110kg"
}, {
name: "Grizly",
weight: "112kg"
}, {
name: "BooBoo",
weight: "200kg"
}]
Then you can go ahead and iterate through it using a for loop inside of the render() method like this.
render() {
var bearElements = [];
for (var bearIndex = 0; bearIndex < bears.length; bearIndex++) {
bearElements.push(
<p>{`${bears[bearElements].name}` `${bears[bearElements].weight}`}</p>
)
}
return (
<div>{bears}</div>
);
}
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
I am trying to avoid to repeat each time
$('.element1'),
$('.element1').html(),
in
myList = [
{
element: $('.element1'),
text: element.html(),
value: 'somevalue'
},
{
element: $('.element2'),
text: element.html(),
value: 'somevalue'
}
];
but actually it is not working, it shows me the eroor:
Uncaught ReferenceError: element is not defined
I need to define and keep everything only inside the "myList",
avoiding to define other external variables etc, and I would like to know how I can use something like this
I really appreciate any help
You want to separate your data from your logic a bit more, so that it can scale later
myList = [
{
element: '.element1',
value: 'somevalue'
},
{
element: '.element2',
value: 'somevalue'
}
];
myList.forEach(function(elem) {
$(elem.element).html(elem.value)
});
myList = [
{
element: $('.element1'),
value: 'somevalue'
},
{
element: $('.element2'),
value: 'somevalue'
}
];
for( var i in myList ) {
myList[ i ]["text"] = myList[ i ][ "element" ].html();
}
You have to declare the variable before.
Property/key of objects are not variable themselves in the current scope.
var element = $('.element1');
var element2 = $('.element2');
myList = [
{
element: element,
text: element.html(),
value: 'somevalue'
},
{
element: element2,
text: element2.html(),
value: 'somevalue'
}
];
Also check here for possible one time initialization of the object.
This would not save you so much typing and code.
Self-references in object literal declarations
is not really clear why you want to avoid to repeat them and keep all of them in the object, of course saving the processing of jquery functions is a reason.