Hello, I want to know if its possible to run some kind of script to fill titles:
This is the format that I need:
titles: [
{ title: "Germany" },
{ title: "Italy" },
{ title: "Belgium" }
]
I have this code that get’s data from my database and converts it to an object with data inside:
const getTables = {};
db.row.forEach(calcResult);
function calcResult(pair, isjson) {
if(isjson === true) {
getTables[pair.key] = JSON.parse(pair.value);
} else {
getTables[pair.key] = pair.value;
}
}
console.log(getTables);
The object with the data:
https://puu.sh/B7utu/0146d8cb3b.png
How I can create a script that gets all the titles inside the object and adds the format: { title: "object_title" }
So I can run it here:
titles: [
//
]
Thanks!
Using:
console.log(getCountries);
I get this:

The problem is when it prints the texts, it adds automatically the 0, 1, 2, … before the “key” thing. So I can’t access key
Tried this at the moment, and nothing worked:
console.log(getCountries.key);
console.log(getCountries..key);
console.log(getCountries.[].key);
Any idea?
Thanks.
Edit: Here re-created this scenario and still figuring how to access to nested values when there’s number. https://jsfiddle.net/46983ey0/
Edit: I found a way to get the value: console.log(getCountries[0].key);
but I’m only accessing to the key of the 0 string. I need to get all the keys. How I print all the keys dynamically? If there are 3 nested groups, then print 3 keys, if there’s 20, print 20.
What you’ve got is an Array. (see that at the end there?)
So, there are two (major) ways to go about accessing them;
1: If you’re going to do processing on each object individually, run a forEach.
2: To reduce the individual items down to a single value, instead you’re trying to Map the array.
What you sound like you’re trying to do is map the values down to a single value for each
1 Like
I need to get the all the values that are inside “key”. In the image case: Germany, Italy, Belgium.
But that’s actually loading things from the database, so in the case, there are more values it will print more values.
So for this case: which I should use forEach or map?
Thanks!
So… there’s a couple of different ways to go about what you’re trying to do, from rereading your OP.
But if we get to this point:
https://puu.sh/B7utu/0146d8cb3b.png
Then our titles are this object’s keys:
titles = Object.keys(thevariableabove);
(This, however, gives you an Array of Strings, not an array of objects in your desired format.)
If getCountries
is as you have it in your second post, you can map it down to its keys similarly to this example on the Mozilla declaration for map:
titles = getCountries.map((item) => {"title":item.key});
1 Like
Thanks, @m_hutley i just tested now the first solution:
Got this:
https://puu.sh/B7zHs/a4bdf2148d.png
Which is also nested inside 0, 1 arrays.
I’ll check now the second solution.
Thanks!
That’s not nested inside anything, that’s how an Array looks. A numerically indexed array, with value [0] being Germany, and value [1] being Italy.
1 Like
I’m having problem with this:
titles = getCountries.map((item) => {“title”:item.key});
Maybe the => is causing an error? I’m getting a syntax error from my js editor.
If you’re running IE, then it’ll fail because IE cant figure out how to read a specification document and realize that arrow functions are a thing. Other than that, you have defined titles already, yes?
1 Like
I’m using Chrome. Yes, I have defined titles. But I see the item has a blue color that (normally) has the things that should be declared, and in my document, I don’t have anything with the item.
Where is pointing the item?
Thanks.
Sitepoint has an article up about fat arrow functions. Give it a read and see if it explains what’s going on to you.
1 Like
I see, seems is not the => is something else.
I’m trying to find where is the problem on the line.
Thanks.
Great article.
I did this:
const map1 = getTables.map(item => [{"title":item.key}]);
Edit: It’s working now. This is how looks like:
https://puu.sh/B7DUc/b92e7a3433.png
This is how you said?
Thanks!
Edit: Used JSON.stringify(map1);
And this is how looks:
https://puu.sh/B7E2j/69d7cb7abb.png
Any idea how to remove all the claustrators ?? So only:
{"title":"Germany"},{"title":"Italy"},{"title":"Belgium"}
Yayy I feel I’m very close!
Thanks so much!
Well… simply dont put the 's into the code line.
Compare mine:
titles = getCountries.map((item) => {"title":item.key});
to yours:
const map1 = getTables.map(item => [{"title":item.key}]);
spot the excess 's.
Map automatically returns an array (It “changes” the values of an array, giving you the “modified” array back.) [actually it creates a new array of the same length as the old one], so you dont need to specify that the elements are array pieces.
After you stringified it, use replace to remove the square brackets.
map1 = map1.replace(/[\[\]]/g,'');
I think that’s how it’s done. Been a while. (Or, do what m_hutley suggested.) 
V/r,
^ _ ^
That’s exactly what i tried, but if i remove that squares that are wrapping the {"title…} it gives me syntax error and it marks red the whole line.

Oh right. I forgot arrow functions have to handle curly braces as block level statements rather than object wrappers. Derp.
Wrap it in parenthesis instead.
const map1 = getTables.map(item => ({"title":item.key}));
1 Like
Yeah, that solved the problem:
https://puu.sh/B7Ez4/7219168dc6.png
Thank you so much!!
Now my problem is when I use:
const map1 = getTables.map(item => ({"title":item.key}));
const map2 = JSON.stringify(map1);
......
columns: map2
It should work because map2 equals the last image.
The format is this:
columns: [
{ title: "Germany" },
{ title: "Italy" },
{ title: "Belgium" }
]
I’m getting this error:
x3:8 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [
If I change
columns: map2
to columns: [ map2 ]
The error is:
x3:8 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"title":"Germany"},{"title":"Italy"},{"title":"Belgium"}]
What can cause this?
Thanks!
Edit: May the quotes of title are causing this? “title”: instead of title: ??
Edit2: I tried using static text and the same format as the result and it works with the “title”… so its another thing.
I’m uncertain of your editor’s markup, but it’s either telling you there’s a problem on line 3 or line 8, i’m not sure what the “x” is coming from, if that’s a file name or what.
That’s chrome inspector.
It’s weird. What means: Cannot use ‘in’ operator?
I’m using an ‘in’ operator in this code?:
const getTables = db.row;
const map1 = getTables.map(item => ({"title":item.key}));
const map2 = JSON.stringify(map1);
console.log(map2);
columns: map2