1

I hope the title is enough self-explanatory but I will try to clarify with a fiddle I've created. There are two arrays, one called tags which is what I currently have and tags_ideal which is what I really want to achieve.

Current state:

var tags = [
  {
    id: 1,
    color: 'red',
    l10n: [
      {
        name: 'Something something',
        lang: 'english'
      },
      {
        name: 'Etwas etwas',
        lang: 'deutsch'
      }
    ]
  },
  {
   id: 2,
   color: 'blue',
   l10n: ...
  }
]

What I'm after:

var tags_ideal = [
  {
    id: 1,
    color: 'red',
    l10n: {
      'english': {
        name: 'Something something',
      },
      'deutsch': {
        name: 'Etwas etwas',
      }
    }
  },
  {
   id: 2,
   color: 'blue',
   l10n: ...
  }
]

I have the first case and want to convert all the stuff from l10n so that they don't have a lang: english parameter but rather have an object called english and the name/title inside of that. Below both tags is what I tried to do and what works when I pass just the l10n object into it, but not the whole thing (I do understand why, and now I would like to know how to do what I am really after).

Also, please note that my arrays are not correct at this point. I have appended three dots at the start of the next object just to point out that there is more than one object in my array.

Here's the fiddle: https://jsfiddle.net/cgvpuj70/

3 Answers 3

2

You could iterate the outer array and map new objects for the inner arrays.

var tags = [{ id: 1, color: 'red', l10n: [{ name: 'Something something', lang: 'english' }, { name: 'Etwas etwas', lang: 'deutsch' }] }, { id: 2, color: 'blue', l10n: [] }];

tags.forEach(a => a.l10n = a.l10n.map(b => ({ [b.lang]: { name: b.name } })));

console.log(tags);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Although I'm getting the same results as below, I've marked spanky's answer as correct since it works without having to modify anything to bypass eslinter. Thanks a lot!
1

Use .reduce() on the l10n to make an object populated with the keys/values derived from each object in the current Array.

var tags = [
  {
    id: 1,
    color: 'red',
    l10n: [
      {
        name: 'Something something',
        lang: 'english'
      },
      {
        name: 'Etwas etwas',
        lang: 'deutsch'
      }
    ]
  },
  {
   id: 2,
   color: 'blue'
  }
]

tags.forEach(t => {
  t.l10n = t.l10n && t.l10n.reduce((o, data) => 
    Object.assign(o, {[data.lang]: {name: data.name}})
  , {})
})

console.log(tags);

2 Comments

Awesome! Works like a charm.
Glad it helped!
0

All you have to do is create an empty object and fill it up. First, create the new object. Then, for each element of the l10n array, set object[element.lang] equal to element.name. Now you've go the new json structure.

Here's a modified translate function:

function translate(title) {
  var object = {};
  for (var i=0; i<title.length; i++) {
    var element = title[i];
    object[element.lang] = element.name;
  }
  return object;
}

Edit: Here's how you can translate the entire object

var newTags = [];
for (var i=0; i<tags.length; i++) {
    var edited = {};
    edited.id = tags[i].id;
    edited.color = tags[i].color;
    edited.l10n = translate(tags[i].l10n);
    newTags.push(edited);
}

1 Comment

It seems to me that this is only working if I pass l10n into it, but not the whole tags var?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.