I'm looking to extract dict values by key, and I've attempted to add those values to an empty array using the concat() function, however it's printing the values but within their own arrays (atleast it appears that way since each value is surrounded by unique sets of brackets).
var dict = {Name: 'Chris', Height: 150, Location: 'New York'};
var dictVal = new Array();
for (var key in dict) {
var val = dict[key];
console.log(dictVal.concat(val));
}
How do I merge the values so they live within their own single set of brackets to denote the list of values within dictVal?
The mistake that you're making is that Array.concat returns a new array meaning
dictVal = dictVal.concat(val);
is what you want in order to get the result that you want. Alternatively you can also do
for (var key in dict) {
var val = dict[key];
dictVal.push(val);
console.log(dictVal);
}
if you don't want to generate new arrays.
Moreover, there are better ways to do what you want for example mapping the keys of the object to the values:
var dict = {Name: 'Chris', Height: 150, Location: 'New York'};
var dictVal = Object.keys(dict).map(key => dict[key]);
Iterating through Object.keys is usually preferable to a for...in because
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
and Array.map just saves you several lines of code.
Also as a side note using new Array() is generally not common. You'd want to do
var dictVal = []
just to be in better accordance with common JS conventions.
You may do as follows in modern browsers;
var dict = {Name: 'Chris', Height: 150, Location: 'New York'},
dictValues = Object.values(dict);
console.log(dictValues);
Related
I don't know if this is my little knowledge of jQuery or it is just a bug, but here's what happens. I have this small piece of JSON code
{
"planes":[
{
"id":1,
"name":"Boeing 767-300",
"height":54.9 ,
"wingspan":47.6,
"vel": 851,
"vel max":913,
"plane width":283.3,
"weight":86070,
"full weight":158760,
"passengers":{
"1 class":350,
"2 class":269,
"3 class":218
},
"fuel tank":90.625,
"engine":"2 turbofan General Electric CF6-80C2"
},
{
"id":2,
"name":"Boeing 737-800",
"height":33.4 ,
"wingspan":35.8,
"vel": 840,
"vel max":945,
"plane width":105.44,
"weight":32704,
"full weight":56472,
"passengers":{
"1 class":189
},
"fuel tank":90.625,
"engine":"2 turbofan CFM56-3C1"
}
]
}
which I'm then getting with jQuery's getJSON without any flaw. Then I want two separate arrays: one holding the keys and the other holding the values, and again no problem with Object.keys and Object.values. By logging the result in a single string, everything is fine. Until I try to construct an associative array using keys as indexes and values as data. By logging the result, I get an extra "length" index with value "0". here's my jQuery code
var arr=[];
$.getJSON("js/jsondata.json", function(data){
var keys= Object.keys(data.planes[0]);
var values= Object.values(data.planes[0]);
//im only testing on the first object, for now
$.each(keys, function(i){
//creating the associative index and assigning the value
arr[keys[i]]= values[i];
console.log("Key: "+ keys[i]+", Value: "+values[i]);
//this logs the exact values and indexes
});
console.log(arr);
//this logs an extra "length" 0
});
What you really want to use is a key-value object and not an array. So you have at least to options:
Actually the arrays are objects, and you will be able to attach/add new properties, however, this kind of objects have a pre-defined prototype and properties. One of these properties is length. Cause that, you're getting an "unexpected" property length.
Changing this var arr = []; to this var arr = {};.
Changing this var arr = []; to this var arr = Object.create(null);.
Adding properties to an object array
let arr = [2];
arr['myKey'] = 'EleFromStack';
console.log(arr.myKey);
console.log(arr.length); // 1 cause length is part of Array type.
Adding properties to a key-value object
let arr = {}; // Object.create(null);
arr['myKey'] = 'EleFromStack';
console.log(arr.myKey);
console.log(arr.length); // undefined cause length is not part of the Object type.
Biggest problem is there's no such beast as associative arrays in JavaScript. All arrays must have numbered indices. Association the way you want is handled with objects.
So, you can just assign the first plane in your planes array to a variable and retain your original association rather than iterate it.
Is there a particular reason you're trying to disassemble and reassemble your object to an array this way?
I created an array with many elements with a loop:
myArray = [c1, c2, c3...]
now I want to make each element into an object and assign different key values:
c1 = {image = path, value = number)
I tried to run forEach() but can't figure out the correct way to do so and I have not succeeded in finding the answer to it.
My guess was:
myArray.forEach(function() {
let name = {
image = path,
value = number,
}
return name;
});
but there's no change in the elements in the log.
Any help or link to an answer that can help me here. First time coding here.
UPDATE: an easier solution was to .push all the keys and values of the objects when I created the array with the loop in the first place.
array.push({image: pathx, value: numberx})
You can, but you'd be better off with a simple for loop:
for (let i = 0; i < myArray.length; ++i) {
let entry = myArray[i];
myArray[i] = {image: entry.path, value: entry.number};
}
Or making a new array with map.
newArray = myArray.map(entry => ({image: entry.path, value: entry.number}));
Or if you prefer non-arrow functions:
newArray = myArray.map(function(entry) {
return {image: entry.path, value: entry.number};
});
You could theoretically push to a new array but this is the exact usecase for Array#map. Array#map maps old values to new values. The returned object from the callback is the new object and the returned array is the new array containing the new objects.
Semantically, Array#forEach is to plainly iterate over each element in an array, and possibly execute something with side-effects (which may include pushing to another array). But with Array#map, it's specifically used to transform old array values to new ones. Use the one that is specifically designed because it conveys a clear message to anyone else who reads your code.
const newArray = myArray.map(({ path, number }) => ({
image: path,
value: number
}));
Array#map maps old values to new values. You may need to use the follwing instead of arrow functions as it is not supported in IE.
I just added dummy data in the object, you can change it.
myArray = ["c1", "c2", "c3"]
myArray = myArray.map(function(elem) {
return {"image":"path"+elem,"value":"value"+elem};
});
console.log(myArray);
I have a code :
var index = 100;
var arr =[];
arr[index.toString()] = "Hello"
The result : index still known as integer not a string. Anyone can explain what's wrong with my code?
You have to declare associative arrays using {}, which creates a new object, because in JavaScript, arrays always use numbered indexes.
You need to declare an object: var arr={};
arrays use numbered indexes.
objects use named indexes.
var index = 100;
var arr ={};
arr[index.toString()] = "Hello";
console.log(arr);
How to make associative array with number as string in Javascript
JavaScript doesn't have associative arrays in the sense that term is frequently used. It has objects, and as of ES2015 (aka "ES6"), it has Maps.
The result : index still known as integer not a string. Anyone can explain what's wrong with my code?
The index variable's value is still a number, yes, because you haven't done anything to change it. But the index in the array is a string (and would be even if you didn't use .toString()), because standard arrays aren't really arrays at all1, they're objects with special handling of a class of properties (ones whose names are strings that fit the spec's definition of an array index), a special length property, and that use Array.prototype as their prototype.
Here's proof that array indexes are strings:
var a = [];
a[0] = "zero";
for (var name in a) {
console.log("name == " + name + ", typeof name == " + typeof name);
}
That said, you don't want to use an array when you want a generic object or map.
Here's using a generic object for name/value mappings:
var o = Object.create(null);
var name = "answer";
o[name] = 42;
console.log(o[name]); // 42
The property names in objects are strings or (as of ES2015) Symbols. I used Object.create(null) to create the object so it wouldn't have Object.prototype as its prototype, since that gives us properties (toString, valueOf, etc.) that we don't want if we're using the object as a map.
Here's using a Map:
var m = new Map();
var name = "answer";
m.set(name, 42);
console.log(m.get(name)); // 42
The main advantages Maps have over objects are:
Their keys can be anything, not just strings or Symbols
They're iterable, so you can use for-of to loop through the mappings they contain
Maps have a size property telling you how many entries they have
Maps guarantee that iteration of their entries is performed in the order the entries were added to the map
With ES6, you could use a Map, which holds any type as key.
var map = new Map;
map.set(100, "Hello");
map.set('100', "Foo");
console.log(map.get(100)); // 'Hello'
console.log(map.get('100')); // 'Foo'
console.log([...map]);
JavaScript does not support arrays with named indexes, in JavaScript, arrays always use numbered indexes.
If you use a named index, JavaScript will redefine the array to a standard object.
After that, all array methods and properties will produce incorrect results.
As you can see in the following example:
var person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
var x = person.length; // person.length will return 0
console.log(x);
var y = person[0]; // person[0] will return undefined
console.log(y);
How do you sort an object by keys based on another array in Javascript?
This is the same question as here PHP Sort an Array by keys based on another Array?
But I need the same process for Javascript.
Thanks
The JavaScript specification does not require that object keys maintain their order, so it is not safe to attempt to sort them. It is up to each environment to implement the standard, and each browser does this differently. I believe most modern browsers will sort keys on a first-in-first-out basis, but since it is not part of the standard, it is not safe to trust this in production code. It may also break in older browsers. Your best bet is to place your object keys into an array, sort that array and then access the values of the object by the sorted keys.
var myObject = { d: 3, b: 1, a: 0, c: 2 },
sortedKeys = Object.getOwnPropertyNames(myObject).sort();
You could then map the ordered values into a new array if you need that.
var orderedValues = sortedKeys.map(function (key) { return myObject[key]; });
As QED2000 pointed out, even if you create a new object, there's no guaranty that the properties will remain in a specific order. However you can sort the keys depending on a different array.
<script>
function sortArrayByArray(a, b)
{
return order.indexOf(a) - order.indexOf(b);
}
var order = new Array('name', 'dob', 'address');
var customer = new Array();
customer['address'] = '123 fake st';
customer['name'] = 'Tim';
customer['dob'] = '12/08/1986';
customer['dontSortMe'] = 'this value doesnt need to be sorted';
var orderedKeys = Object.keys(customer).sort(sortArrayByArray);
orderedKeys.forEach(function (key) {
document.write(key + "=" + customer[key] + "<br/>");
});
</script>
The output is
dontSortMe=this value doesnt need to be sorted
name=Tim
dob=12/08/1986
address=123 fake st
I have a two dimensional array which I created like this
var books = new Array(); // create an Array
var book1 = new Array();
book1.push("1");
book1.push("adfgsdg");
book1.push("dfgsdfg");
book1.push("dfgds");
book1.push("44.95");
book1.push("dfgsd");
book1.push("dfgsdg");
books.push(book1);
var book2 = new Array();
book2.push("2");
book2.push("gdhff");
book2.push("fghfd");
book2.push("fghdf");
book2.push("44.95");
book2.push("2000-12-16");
book2.push("fghfghd");
books.push(book2);
can you tell me how to dynamically create a new book3, book4.... array and push into the books array.
Just use an object:
var books = {book1: ['book1 title', 'book1 - title2']};
books['book2'] = ['book2 titles'];
books.book3 = [];//works, too
books.book3.push('foobar');
If you insist on having variables that reference a particular array, that's very easily done:
var book2 = books.book2;//arrays are objects, so:
book2.push('A second title');
console.log(books.book2[1]);//logs "A second title"
Easy-peasy. on the dynamic names front, just a quick example:
books['book' + someVar] = [];
Works just fine.
As #raghaw pointed out, perhaps you could do with some explaining:
JavaScript doesn't really support associative arrays as such, instead objects are used (even Array's are just "pimped" objects, but that's a different matter)
You can create a new instance of any object by calling the construct (new X(), like you did), but in case of native object types (like Array and Object), that's not to be recommended (new Array(10); and new Array('10') behave differently, for example).
In those cases the literal notation is to be preferred:
var wrong = new Object();//not "wrong", but impredictable
var better = {};//or new [whateverTypeOfObject]();
//ditto for arrays:
var dangerous = new Array();
var safe = [];
To assign an object/array with a certain set of values already there, simply fill them in:
var myObject = {key: "value",
theValue: {can: 'be',
anything: ["even", "another", "object"]}
};
The data can be accessed both by using the dot-notation or the bracket notation:
console.log(myObject.theValue['can']);//be
//===
console.log(myObject.theValue.can);//be
//===
console.log(myObject["theValue"]["can"]);//be
If you're using variables as keys, you'll have to use the bracket notation. If you feel like you need more info check out MDN, spend some time on that site - it's a good reference on JS
First of all I think that the answer by Elias is very good. However, if you are not in the mood of going OO, just create books 3 and 4 as raghavv told in the comment, then push them all in the books array
books.push(books1);
books.push(books2);
books.push(books3);
books.push(books4);
The result of it will be actually something like
var books[
books1[value1, value2, value3],
books2[value1, value2, value3],
books3[value1, value2, value3],
books4[value1, value2, value3]
];