is this:
var arr = {};
the same as
var arr = new Array();
?
It is not the same.
var arr = {};
initializes an object. If you want an array:
var arr = [];
not exactly. var arr = []; is more like it.
No.
var arr = {}; // creates a new object with no properties
But
var arr = []; // creates a new blank array
var arr = {}; creates object
No, it's not: You're confusing array literals
var arr = []; // same as new Array()
with object literals
var obj = {}; // same as new Object()
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
I have array like this:
var data = [[]];
var dataIndex = ['a','b','c'];
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
I want to make that array to be like this:
data['a'] = [1,2,3];
data['b'] = [4,5,6];
data['c'] = [7,8,9];
I've tried with this code, but it doesn't work.
for(var i=0;i<dataIndex;i++){
data[data[i]].push('data'+i[i]);
}
I don't know to do that in javascript since I'm new on this. Anyone can help me? thank you.
By Using function and loop.
var data = {};
var dataIndex = ['a','b','c'];
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
function covert(){
const allData = [data0,data1,data2]
dataIndex.forEach((item,i)=>{
data[item] = allData[i];
})
}
covert();
console.log(data)
Consider using an object(associative array) to save the values
Define your data object as follows
var data = {}
// You can now do the insertion operations as follows
data['a'] = [1,2,3]
// data in your object will be as follows
{'a': [1,2,3]}
You can just do it like this then. No need for loops
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
var data = {
a : data0,
b : data1,
c : data2
};
I have a string:
var string = "test,test2";
That I turn into an array:
var array = string.split(",");
Then I wrap that array into a larger array:
var paragraphs = [array];
Which outputs:
[['test','test2']]
But I need it to output:
[['test'],['test2']]
Any clue how I can do this?
Just map each item into an array containing that item:
var string = "test,test2";
var result = string.split(",").map(x => [x]);
// ^--------------
console.log(result);
let test_string = "test,test2";
let result = test_string.split(',').map(item => [item])
console.log(result)
You can get expected result using array Concatenation.
var string = "test,test2";
var array = string.split(",");
var finalArray=[[array[0]]].concat([[array[1]]])
console.log(JSON.stringify(finalArray));
What version of JavaScript are you targeting?
This might be a general answer:
var arr = "test,test2".split(",");
var i = arr.length;
while(i--)
arr[i] = [arr[i]];
I having the Object with Date as Key and Value as Locations. I want Get the locations based on the largest Date. Here the locations are same. The Date Key is in "MM/DD/YYYY" Format.
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
Form the above object the largest Date(Key) is "9/25/2016" and the value would be "India-Chennai" How to get the value based on the largest key?
Thanks in Advance
You can use Object.keys(), Array.prototype.map(), Math.max(), Date.prototype.getTime(), Array.prototype.indexOf()
var obj = {};
obj['9/10/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
obj['9/20/2016'] = "India-Delhi";
obj['9/25/2016'] = "India-Chennai";
var keys = Object.keys(obj);
var map = keys.map(function(date) {return new Date(date).getTime()});
var key = Math.max.apply(Math, map);
var value = obj[keys[map.indexOf(key)]];
console.log(value);
You might do something like this;
var obj = {},
latest = [];
obj['9/20/2016'] = "India-Delhi";
obj['9/10/2016'] = "Japan-Osaka";
obj['9/25/2016'] = "India-Chennai";
obj['9/15/2016'] = "Australia-Melborne";
latest = obj[Object.keys(obj).sort((a,b) => new Date(b).getTime() - new Date(a).getTime())[0]];
console.log(latest);
You can use Object.keys() to get a list of keys as an array, then .sort() that array:
var obj ={};
obj['9/10/2016'] = "India-Chennai";
obj['9/9/2016'] = "Australia-Melborne";
obj['9/25/2016'] = "India-Chennai";
obj['9/20/2016'] = "India-Delhi";
var parseDate = d => (d = d.match(/(\d{1,2})\/(\d{1,2})\/(\d{4})/)) && new Date(d[3],d[1]-1,d[2]);
var latestKey = Object.keys(obj).sort((a, b) => parseDate(a) - parseDate(b)).pop();
var result = obj[latestKey];
console.log(latestKey, result);
I've used a regex to parse the dates, so that the code can easily be adapted to other date formats, e.g., if you needed d/m/yyyy instead of m/d/yyyy.
How to push array in another array using JavaScript?
Example:-
var a = [1,2,3,4,5]
var b = [6,7,8,9,10]
i want to push a & b into c
var c =[[1,2,3,4,5],[6,7,8,9,10]]
Basically what sam said is enough:
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);
To convert to JSON:
var myJsonString = JSON.stringify(yourArray);
And to get it into an object :
var arrFromJson = JSON.parse( myJsonString );
Simple, you just have to create another array to do it:
var c = [a,b];
Then, you can use .push to add more arrays.
c.push([11,12,13]);
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);
I'm looking to stringify an object.
I want in output like this
{"1":{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, "2": {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}}
But I get this
{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}
What I do
var objVal = {}; //value....
var data = {}; //other value....
var object = $.extend({}, objVal, data); //concat the object
JSON.stringify(object);
When you concat the object, you get an array; you want a map with two elements, using the id "1" and "2"
var objVal = {}; //value....
var data = {}; //other value....
var object = {}
object["1"] = objVal;
object["2"] = date;
JSON.stringify(object);
I found the solution !
I do an for loop on the object. And I iterate on each element in the object. Thank you for your help. The answer of #Giovanni help me to found the solution.
Solution:
var data = {}; //values....
var objVal = {}; //other values....
var final = {};
var index = 1;
for(var key in data)
{
final[index] = data[key];
index = index + 1;
}
final[index] = objVal;
JSON.stringify(final);
And the output is :
{"1":{"valeur":"dfgdfg","usager":"experttasp","date":"2013-08-23 10:36:54"},"2":{"valeur":"uuuuuuuuuu","commentaire":"defg","usager":"experttasp","date":"2013-08-23 10:37:26"},"3":{"valeur":"uuuuuuuuuu","commentaire":"yesssss","usager":"experttasp","date":"2013-08-23 10:38:38"}}