Printing/console logging from map object [duplicate] - javascript

This question already has answers here:
How to iterate over Object's property-value pairs?
(8 answers)
Closed 1 year ago.
Lets say I have personalRecords parameter.. and it accepts this map object:
new Map([['100', 9.58], ['200', 19.19]])
I want to take these numbers and print them out / console.log in VSCode in such format:
100m: 9.58s
200m: 19.19s
I can't seem to figure out how to take these numbers out of the object and print them out properly. I appreciate your help in advance.

You can use for each to achieve that
const personalRecord = new Map([['100', 9.58], ['200', 19.19]])
for(let record of personalRecord){
console.log(record[0] + "m: " + record[1] + "s")
}

Related

Can we do something like mapName[key]++ in javascript like we can do in C++ [duplicate]

This question already has answers here:
Javascript counting the frequency letters of a string
(4 answers)
Closed 7 months ago.
I'm new at JS. When I work on frequency maps in C++, I can do something like:
map<char,int> newMap = {{'a',1}, {'b',2}, {'c',3}, {'d',4}};
newMap['a']++;
And this increases the value corresponding to 'a' to 2 from 1
But I can't do:
let m = new Map()
m.set('a',1)
m.get('a')++
Can anybody tell me a workaround for this?
Thanks
If you're using a Map, there is no way to change primitive values in a Map other than calling map.set each time. But if you were using a plain object instead, you could use shorthand syntax very similar to what you're familiar with in C++.
const m = { a: 1 };
m.a++;
console.log(m);

JavaScript How to declare 16 length array with default 0 value quick? [duplicate]

This question already has answers here:
Most efficient way to create a zero filled JavaScript array?
(45 answers)
Closed 3 years ago.
let array1 = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
how to make like this array with shortest way ? is it possible to make shorter way ?
thank you for your answers.
You can use the .fill() method:
let array1 = new Array(16).fill(0);
As its name suggests, it fills the array with some desired value.

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

how to findIndex of [duplicate]

This question already has answers here:
How to find the array index with a value?
(12 answers)
Closed 5 years ago.
I'm wondering how to get the findindex of a specific service_name in the below array?
var obj = {"ID":111222,"NAME":"Chicken","FREQUENCY":"Yearly","service_name":["Open ticket","Service Account time","Assets Management Overview"]}
I have tried the following but not getting the correct index
var find_index = obj.service_name.findIndex(function(obj){return obj.service_name = "Open ticket"})
console.log ("The index of " + find_index);
Your code is close, but there are a couple of semantic issues. You're already iterating over the service_name array so the argument that the .findIndex callback takes is the array element / string itself. You also need to use == or === for comparison. Right now you are performing assignment.
const find_index = obj.service_name.findIndex(
serviceName => "Open ticket" === serviceName
);
In this case you can also use .indexOf which would be a bit simpler:
const index = obj.service_name.indexOf("Open ticket");

Swap values of keys in JSON array [duplicate]

This question already has answers here:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle

Categories

Resources