This question already has answers here:
JavaScript: How to join / combine two arrays to concatenate into one array?
(3 answers)
Closed 4 years ago.
I have a newbie question regarding arrays and objects. I have an array of objects that I would like to push into another array of objects, but can't seem to get it to work correctly.
For instance, if I have data.list:
data.list=[{
a,
b,
c
}];
and I want to push data.list into another object array called data.overall so that it looks like this:
data.overall=[{
data.list,
z,
y,
x
}];
Sorry, for clarification, I want data.list to still exist as an array within data.overall. I've tried concat, but I keep getting an error in the console saying Server JavaScript error Cannot find function concat in object [object Object].
Thanks!
As you said, newbie I am trying to help you,
Javascript Objects should have always properties like below and you wanted to achieve this? Let me know if you have any questions.
From your question, I assume that you wanted to achieve this, Hope this helps to proceed next with Javascript
And also you can always check how JSON should be structured with below URL
https://jsoneditoronline.org/
var data = [{
"a": 1,
"b": 2,
"c": 3
}];
var dataNew = [{
"x": 4,
"y": 5,
"z": 6
}]
function getData(data) {
return data;
}
var modiFiedObject = [{
"data": getData(data[0]),
"dataNew": getData(dataNew[0])
}]
console.log(modiFiedObject);
Related
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 4 years ago.
Please consider two thing before any comment/answer/vote
Object is within array
I know I can achieve it with constructor or
function by binding this but I have a condition where I have to
rewrite almost 100 lines of existing code just to achieve above
functionality. I search almost everything and now asking from Java
script Enthusiasts about all possible options with same domain
Look at the below object.
Question:I just want total value to be equal to 7 which in current example is NAN
var demoObj = [{
a: 2,
b: 5,
total: this.a * this.b
}]
how I can achieve it? Please answer with any possible solution not matter how ugly code are I will manage.
Edit Someone try to mark it as duplicate to a given link in comments which is not true because there given answers not suite my needs. even in accepted answer there, if you change the value of a or b after initialization then total will change which not suite my case !
Can you use an IIFE or it is too ugly / out of the scope of your question?
var demoObj = [
((a, b) => ({ a, b, total: a * b }))(5, 2)
]
console.log(demoObj)
// [
// {
// "a": 5,
// "b": 7,
// "total": 10
// }
// ]
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I did not find the syntax to do this.
Some unsuccessful attempts:
TILESETS[actualTileSetNO].tiles
One inconvenient but working way:
//access an object which is a specific row of the array
let x=TILESETS[actualTileSetNO];
//access the array that is within the object
let y=x.tiles;
__ update:
My syntax was good, but I think the property was not existing yet (loaded by a promise). It works if I do:
if (typeof(state.TILESETS[state.actualTileSetNO]) !=='undefined') {
return state.TILESETS[state.actualTileSetNO].tls}
else {return null}
I do not see a way to close the case or flag it as an answer.
Checkout the following code snippet -
Assuming this might be the array structure.
let arr = [{
arr1: ['a', 'b', 'c', 'd']
}]
console.log(arr[0].arr1);
if arr is your main array which has a couple of objects in it. Access them by using the index which will fetch you that particular object and within it, to access another array, you can use it by referring it to it's key.
If you have an array of objects, you should be able to access the properties of the object using the syntax you described:
TILESETS[actualTileSetNO].tiles;
e.g
const TILESETS = [
{ tiles: [1, 2, 3] },
{ tiles: [2, 4, 5] }
];
console.log(TILESETS[0].tiles[1]);
// prints 2
If this doesn't work for you, perhaps the problem is elsewhere. Could you post more of your code?
did you mean this here? let me know if this helps you!
arrName[arrItem].objectPropertyName[subArrItem]
var myArr = [
{
a:[1,2,3,4],
b:[5,6,7,8],
c:[9,10,11,12]
},
{
d:[13,14,15,16],
e:[16,18,19,20],
f:[21,22,23,24]
}
]
console.log(myArr[1].f[3])
This question already has answers here:
How to add new property with same key name inside declared object?
(3 answers)
Closed 6 years ago.
I have a json variable like this
var jsondata={"key1":"val1", "key2":"val2"}
I want to push another object with same existing key, and i want that my variable will be like this
var jsondata={"key1":"val1", "key2":"val2", "key1":"val3"}
I tried jsondata["key1"] = "val3", but it didn't return the wanted result
Thank you in advance.
you cannot, as it is a map.
but you could create this json :
var jsondata={"Name":["Jhon","James"], "Age":40}
You can't use the same key in an object. Your question suggests that the logic behind your data structure is wrong.
An alternative:
Use a different field name, i've used "_Name" below, but perhaps "Second_Name" would be more appropriate. Unsure what your json data is modelling.
var jsondata={"Name":"Jhon", "Age":40, "_Name":"James"};
Or perhaps it makes sense to store an array of people, is that what you're trying to achieve? i.e. you have two people, with the names "Jhon" and "James"?
var jsondata={
"people": [
{"Name":"Jhon", "Age":40},
{"Name":"James"}
]
};
This question already has answers here:
Javascript Array Concat not working. Why?
(7 answers)
Closed 6 years ago.
I know this has been asked a lot of times, but I cannot get it to work.
I have an empty array a
var a = [];
and an array with an object b
var b = [{
title: 'test'
}]
I want to join them so a will look exactly like b.
The idea is to do this inside a for loop so a would be added a new item each time.
By using a.concat(b), a results in an empty array.
Not sure what I am missing.
Per Array.prototype.concat()
This method does not change the existing arrays, but instead returns a new array.
You need to assign this operation back to a
a = a.concat(b)
You need to assign result of that call to a. a = a.concat(b)
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);