0

I have an array which has multiple keys name and different keys for abteilung:

{abteilung: "Research & Development", name: "Susanne Gast"}, {abteilung: "HR", name: "John Doe"}, {abteilung: "HR", name: "Joe Doe"}...

Now I want to add a key target with a unique id for every name. And I also want to add a key source with an id for every key+value abteilung. There are duplicates for abteilung.

I'm able to add the key+value target. But how can I add the key+value for abteilung

let linksArray = links;
let u = 0, let a = 0, ln = linksArray.length; 
for (u;u<ln;u++){ 
 linksArray[u].target = u+1;
}

Thank your for your hints

asked Jul 14, 2019 at 12:35
4
  • 5
    What you have is just an array of objects, no JSON in sight. JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. (If what you have in your first code block were JSON, it would be invalid JSON, as property names must be in double quotes in JSON.) Commented Jul 14, 2019 at 12:37
  • "I'm able to add the key+value target. But how can I add the key+value for abteilung" I don't understand. There already is a property called abteilung. You don't need to add it, it's already there. Commented Jul 14, 2019 at 12:38
  • It's not clear what you want the "source" value to be. Commented Jul 14, 2019 at 12:38
  • ok sorry, I need an unique id (key: target) for every name in the array. And i also need a key called source, which should have a unique id. Not in the same range of the target ids because I need the target, source-keys for drawing lines in a graph Commented Jul 14, 2019 at 12:43

1 Answer 1

1

For assigning the source, you could first build a Map that has a key for every unique abteilung. The Map values can then become the sequential number. Finally perform a look-up in that Map for each object and assign the retrieved number to the source property:

const links = [{abteilung: "Research & Development", name: "Susanne Gast"}, {abteilung: "HR", name: "John Doe"}, {abteilung: "HR", name: "Joe Doe"}];
const map = new Map(links.map(o => [o.abteilung, 0]));
Array.from(map.keys(), (abteilung, i) => map.set(abteilung, i+1));
links.forEach(o => o.source = map.get(o.abteilung));
console.log(links);

I did not include the assignment to target, as you had that working fine already.

If it is not necessary that the numbering is a sequence without gapes, but gaps are allowed, then you can also assign the sequence number during the Map construction:

var links = [{abteilung: "Research & Development", name: "Susanne Gast"}, {abteilung: "HR", name: "John Doe"}, {abteilung: "HR", name: "Joe Doe"}];
let i = 0;
const map = new Map(links.map(o => [o.abteilung, i++]));
links.forEach(o => o.source = map.get(o.abteilung));
console.log(links);

answered Jul 14, 2019 at 12:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.