0

I'm new to Javascript, so this is probably fairly easy.

I have this JSON:

var collection = {
    2548: {
      album: "Slippery When Wet",
      artist: "Bon Jovi",
      tracks: [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    2468: {
      album: "1999",
      artist: "Prince",
      tracks: [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    1245: {
      artist: "Robert Palmer",
      tracks: [ ]
    },
    5439: {
      album: "ABBA Gold"
    }
};

And this function:

// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

function updateRecords(id, prop, value) {
  if (value !== "" && prop !== "tracks"){
    return collection.id.prop.value;
  } else if (value !== "" && prop === "tracks"){
    return collection.id.prop.push(value);
  } else {
    delete collection.id.prop.value;
  }

  return collection;
}

These are my tests:

updateRecords(5439, "artist", "ABBA");

updateRecords(2548, "artist", "");

updateRecords(1245, "tracks", "addicted to love");

updateRecords(2458, "tracks", "");

What I need to do is write a function that will access the JSON information and add the information if the argument value is non-blank (value !== "") and prop argument is not tracks (props !== "tracks) update or set the value for the argument prop.

Furthermore if the argument prop is tracks (props === "tracks") and the argument value is also non-blank (value !== "") I need to push the value onto the end of the tracks inside of the JSON file.

My question is how could I go about doing this? Mine is obviously failing I understand that I need to delete and push the information, but accessing the correct data is confusing to me, would I do something like: return collection.id.prop.push(value);?

Community
  • 1
  • 1
13aal
  • 1,571
  • 17
  • 46

2 Answers2

1

Take care, in your code you used collection.id.prop.value. This means it will not resolve variables after the dot, because a.b is equivalent to a["b"].

This means you're trying to access a propriety called id on collection, and so forth.

You should instead use collections[id][prop].value.

var collectionCopy = JSON.parse(JSON.stringify(collection));

function updateRecords(id, prop, value) {
  if (value !== "" && prop !== "tracks"){
    return collection[id][prop].value;
  } else if (value !== "" && prop === "tracks"){
    return collection[id][prop].push(value);
  } else {
    delete collection[id][prop].value;
  }

  return collection;
}
Community
  • 1
  • 1
Kroltan
  • 4,812
  • 5
  • 34
  • 56
0

You should use dot notation to access elements that don't have spaces and bracket notation if they do. Since you don't know if "prop" will contain a space or not here, it's best just to use bracket notation.

Let me know if you have any questions.

Given the following data:

// Setup
var collection = {
    2548: {
      album: "Slippery When Wet",
      artist: "Bon Jovi",
      tracks: [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    2468: {
      album: "1999",
      artist: "Prince",
      tracks: [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    1245: {
      artist: "Robert Palmer",
      tracks: [ ]
    },
    5439: {
      album: "ABBA Gold"
    }
};

// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  if((value !== "") && (prop !== "tracks")) {
    console.log("Updating or setting property: ", prop);
    console.log("setting property to: ", value);
    collection[id][prop] = value;
  }
  else if((value !== "") && (prop === "tracks")) {
    collection[id].tracks.push(value);
  }
  else if(value === "") {
    delete collection[id][prop];
  }

  return collection;
}

updateRecords(5439, "artist", "ABBA");
Dan Weber
  • 1,217
  • 1
  • 11
  • 22
  • 1
    Code-only answers are discouraged: while the original asker might understand, some time in the future someone else may need clarification but the question would be too old to be worth asking. – Kroltan Apr 24 '16 at 20:25
  • The problem isn't space or not, but that `something.prop` is equivalent to `something["prop"]`, and not `something[prop]`. – Kroltan Apr 24 '16 at 20:54
  • Okay so why are the `if/else if`'s in double parenthesis? – 13aal Apr 24 '16 at 21:04
  • Good question. It makes it easier to read grouping together the if terms. See this thread: http://stackoverflow.com/questions/22227248/when-should-you-use-parentheses-inside-an-if-statements-condition – Dan Weber Apr 24 '16 at 21:16
  • Refresh page. Had to edit and add it. On a tablet :) – Dan Weber Apr 24 '16 at 21:18