-4

I have initial array, which should be iterated and new object should be created. Initial array is:

let array = [
    {key: "key1", translation: "some text 1"},
    {key2: "key2", translation: "some text 2"},
    {key3: "key3", translation: "some text 3"},
];

Then I iterate it and create object:

const final = {};
const language = "eng";

for (let item of array) {
    final[item.key] = {};
    final[item.key][language] = item.translation;
}

it ends up as object:

{
  key1: {
    eng: "Some text 1"
  },
  key2: {
    eng: "Some text 2"
  },
  key3: {
    eng: "Some text 3"
  }
}

I need this outer keys (key1, key2 and key3) to be surrounded by double-quotes, so the final object becomes:

{
  "key1": {
    eng: "Some text 1"
  },
  "key2": {
    eng: "Some text 2"
  },
  "key3": {
    eng: "Some text 3"
  }
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 3
    Don't get the question?! `JSON.stringify(array)` will do exactly what you want?! – eisbehr Aug 06 '18 at 11:40
  • Your code will fail. As `item.key` isn't true for all conditions inside the loop – BlackBeard Aug 06 '18 at 11:41
  • 1
    " I end up with"...do you? Maybe it's just how whatever you used to inspect it is displaying it, since at that point it's an object, and not JSON. JSON is text. JSON.stringify() will produce a valid JSON string. It's not clear how you came to your conclusion, or what you're actually worrying about. – ADyson Aug 06 '18 at 11:42
  • I edited my question, actually I want final object to have outer keys `doublequoted`, and inner keys (language) should stay `unquoted`. I'm affraid `JSON.stringify(array)` produces string with both outer and inner keys `quoted`, it's not what I asked for. –  Aug 06 '18 at 11:47
  • 1
    Why do you need such strange format? – eisbehr Aug 06 '18 at 11:49
  • 1
    "outer keys doublequoted, and inner keys (language) should stay unquoted"...that's not valid JSON, so it's unclear why you would think that was desirable. What possible reason could you have for wanting that? I can't see how it would help with anything later, all it does it make it less likely that whatever program you're sending the JSON to will be able to read it properly. – ADyson Aug 06 '18 at 11:55
  • Because my translation library (`react-translated`) requires translations object like this: [link](https://pastebin.com/raw/pme5ZziD) –  Aug 06 '18 at 12:03
  • @basic yes and thats what you already have. – Jonas Wilms Aug 06 '18 at 12:05
  • `{ "foo": { "bar": ... } }` is the exact same as `{ foo: { bar: ... } }` or `{ "foo": { bar: ... } }` or `{ foo: { "bar": ... } }` - That's just how objects work... – Andreas Aug 06 '18 at 12:05
  • Ok, I tested it now. It is fine too if all keys are quoted. So I can apply JSON.stringify, but it returns string with all keys quoted. But I don't need string as final, I need object. –  Aug 06 '18 at 12:09
  • 1
    yes, you need to understand the difference between an object, and JSON. JSON is a text representation of the object. When you debug your code, you may also inspect your objects and see a text representation of your object. It may, or may not, happen to also be a JSON representation. But generally, you only need to actively create JSON when you want to send the data somewhere outside your application, e.g. in an AJAX request. If you're just passing it to some more JS code, e.g. as you mention giving it to a React method, then just keep it as an object. – ADyson Aug 06 '18 at 12:24

4 Answers4

1

I think JSON.stringify() you are looking for. Let's have a look here:

let array = [
{key: "key1", translation: "some text 1"},
{key2: "key2", translation: "some text 2"},
{key3: "key3", translation: "some text 3"},
];
console.log(JSON.stringify(array));
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
  • As I already wrote above, `JSON.stringify` produces string where all keys (both outer and inner are quoted) and that's not what I asked for. However, I don't need string as final output, but object :) –  Aug 06 '18 at 11:48
1

If you use JSON.stringify() to get a JSON string your of your array, the values are perfectly quoted:

let array = {
  key1: {
    eng: "Some text 1"
  },
  key2: {
    eng: "Some text 2"
  },
  key3: {
    eng: "Some text 3"
  }
};

console.log(JSON.stringify(array));
eisbehr
  • 12,243
  • 7
  • 38
  • 63
1

The loop you've written is not correct. It should be something like this:

let array = [
{key: "key1", translation: "some text 1"},
{key2: "key2", translation: "some text 2"},
{key3: "key3", translation: "some text 3"},
];

const final = {};
const language = "eng";

for (let item of array) {
    final[Object.values(item)[0]] = {};
    final[Object.values(item)[0]][language] = item.translation;
}

console.log(JSON.stringify(final));

Also, don't forget to stringify using JSON.stringify().

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
0
let array = [
    {key: "key1", translation: "some text 1"},
    {key: "key2", translation: "some text 2"},
    {key: "key3", translation: "some text 3"},
];

    let data={};
    array.map(item => {
        let d=item.key;
        let da=item.translation;
        data[d]=da;
    })
}

console.log(data)
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
vinoth s
  • 178
  • 6
  • 1. How is this different to the working example in the question? 2. That's not how `.map()` should be used. – Andreas Aug 06 '18 at 14:05