0

When i'm trying buildGraph function with normal function its working properly but with arrow function getting empty obj as output. Help needed.

var roads = [
    "Alice's House-Bob's House", "Alice's House-Cabin",
    "Alice's House-Post Office", "Bob's House-Town Hall",
    "Daria's House-Ernie's House", "Daria's House-Town Hall",
    "Ernie's House-Grete's House", "Grete's House-Farm",
    "Grete's House-Shop", "Marketplace-Farm",
    "Marketplace-Post Office", "Marketplace-Shop",
    "Marketplace-Town Hall", "Shop-Town Hall"
   ];

let buildGraph = (edges) => {
    let obj = {}
    let addEdge = (from, to) => (
        obj[from] === null ? obj[from] = [to] : obj[from]?.push(to)
    )
    edges.map(r => r.split("-")).map(([from, to]) => addEdge(from, to))
    return obj;
}
console.log(buildGraph(roads))   // Output: {}

4 Answers 4

2

Replace null with undefined. If a key is not found in an object it returns undefined not null.

var roads = [
  "Alice's House-Bob's House",
  "Alice's House-Cabin",
  "Alice's House-Post Office",
  "Bob's House-Town Hall",
  "Daria's House-Ernie's House",
  "Daria's House-Town Hall",
  "Ernie's House-Grete's House",
  "Grete's House-Farm",
  "Grete's House-Shop",
  "Marketplace-Farm",
  "Marketplace-Post Office",
  "Marketplace-Shop",
  "Marketplace-Town Hall",
  "Shop-Town Hall",
];

let buildGraph = (edges) => {
  let obj = {};
  let addEdge = (from, to) =>
    obj[from] === undefined ? (obj[from] = [to]) : obj[from]?.push(to);
  edges.map((r) => r.split("-")).map(([from, to]) => addEdge(from, to));
  return obj;
};

console.log(buildGraph(roads));

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

Comments

2

It's not the arrow function, it's this comparison:

obj[from] === null

The === operator performs an exact type-comparison, and for properties which don't exist on an object the exact type of the property is not null, it's undefined. So that comparison is false and the property is never added to the object.

Compare it to undefined instead:

var roads = [
    "Alice's House-Bob's House", "Alice's House-Cabin",
    "Alice's House-Post Office", "Bob's House-Town Hall",
    "Daria's House-Ernie's House", "Daria's House-Town Hall",
    "Ernie's House-Grete's House", "Grete's House-Farm",
    "Grete's House-Shop", "Marketplace-Farm",
    "Marketplace-Post Office", "Marketplace-Shop",
    "Marketplace-Town Hall", "Shop-Town Hall"
   ];

let buildGraph = (edges) => {
    let obj = {}
    let addEdge = (from, to) => (
        obj[from] === undefined ? obj[from] = [to] : obj[from]?.push(to)
    )
    edges.map(r => r.split("-")).map(([from, to]) => addEdge(from, to))
    return obj;
}
console.log(buildGraph(roads))   // Output: {}

Comments

2

You could also use .reduce(). Its often used if you want to group something together

var roads = [
    "Alice's House-Bob's House", "Alice's House-Cabin",
    "Alice's House-Post Office", "Bob's House-Town Hall",
    "Daria's House-Ernie's House", "Daria's House-Town Hall",
    "Ernie's House-Grete's House", "Grete's House-Farm",
    "Grete's House-Shop", "Marketplace-Farm",
    "Marketplace-Post Office", "Marketplace-Shop",
    "Marketplace-Town Hall", "Shop-Town Hall"
   ];

let result = roads.reduce((obj, road) => {
  let [from, to] = road.split("-")
  obj[from] ? obj[from].push(to) : obj[from] = [to]
  return obj
},{})

console.log(result)

Comments

0

It's because your obj[from] === null comparison

Sometimes the 'double equal' still makes sense:

var roads = [
    "Alice's House-Bob's House", "Alice's House-Cabin",
    "Alice's House-Post Office", "Bob's House-Town Hall",
    "Daria's House-Ernie's House", "Daria's House-Town Hall",
    "Ernie's House-Grete's House", "Grete's House-Farm",
    "Grete's House-Shop", "Marketplace-Farm",
    "Marketplace-Post Office", "Marketplace-Shop",
    "Marketplace-Town Hall", "Shop-Town Hall"
   ];

let buildGraph = (edges) => {
    let obj = {}
    let addEdge = (from, to) => 
        (obj[from] == null ? obj[from] = [to] : obj[from]?.push(to))
    
    edges.map(r => r.split("-")).map(([from, to]) => addEdge(from, to))
    return obj;
}
console.log(buildGraph(roads))   // Output: {}

That way you're comparing to undefined or null in one step

Comments

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.