0

From the database I have something like this coming back.

[{
    "Code": "Yes",
    "Name": "Yes",
    "Name_spanish": "Sí"
}, {
    "Code": "No",
    "Name": "No",
    "Name_spanish": null
}]

and I'm working on switching the name back and forth between english or spanish depending on the key I'm given. However, I can't seem to figure out how to remove the spanish part if it needs to be english and vise versa.

I've tried wrapping a JSON.parse around it and then using the delete objectname.Name but I have had no luck.

Code looks like this

var this.model = viewModel;
var option = viewModel.get('Options');
delete option.Name_spanish;
console.log(option);

Is there a way to do this?

7
  • Your question lacks two important pieces of information: 1. How does the data in your database look and 2. what's the query you use to retrieve it. Please ad this information so it can be answered.
    – Philipp
    Oct 22, 2014 at 18:46
  • 2
    Please show us that actual code you tried. delete obj.prop is the appropriate way to remove a property from a Javascript object if that's what you're trying to do.
    – jfriend00
    Oct 22, 2014 at 18:47
  • @Philipp, I do not have access to the database so I do not know what it looks like. I am only dealing with the javascript part. and I am retrieving it by grabbing it from the backbone view Model viewModel.get('Options')
    – zazvorniki
    Oct 22, 2014 at 18:48
  • You have an array of objects, so you have to find the index and splice it!
    – tymeJV
    Oct 22, 2014 at 18:49
  • Similar to what tymeJV just said, after you parse the object you have an array of objects that contain the name property. So you have to iterate EACH record in your results and delete the element, e.g. delete obj[index].name Oct 22, 2014 at 18:50

1 Answer 1

0

You must iterate your object:

var this.model = viewModel;
var option = viewModel.get('Options');

for(var i = 0; i != option.length; ++i)
{
    delete option[i].Name_es_US;
}

console.log(option);

EDIT Here's a fiddle that demonstrates removing a property, including removing a property dynamically (i.e. property by name stored in a string): http://jsfiddle.net/3vk19fLu/

7
  • +1 (I was just answering this) The object is an Array of objects, you have to access the index of the object first before deleting the property. Oct 22, 2014 at 18:53
  • @zazvorniki I added a fiddle that proves it's gone. either Name_es_US isn't in the property you're looking for, so not sure which is correct. Update your example to more accurately fit what you're trying to do. Better yet, update my jsfiddle example and prove to me that it's not deleting. As you'll see in my fiddle, it's gone! Oct 22, 2014 at 18:58
  • The variable names are the same in my code, but I changed them here to make them simpler and forgot to change the one. Even though the fiddle is working, it does not want to work in my code. I just keep getting everything back. Maybe I am just going at this the wrong way.
    – zazvorniki
    Oct 22, 2014 at 19:01
  • Then provide more information. Clearly delete is the correct answer. It obviously works. fiddle us an example and prove to me that it's not working. Oct 22, 2014 at 19:04
  • I don't have anymore information to provide, if I did I would. All, I can see is that I put it in my code and it's not coming out the way it should. Like I said I think I'm going to the entire situation wrong. Maybe deleting isn't the best option for what I'm trying to accomplish.
    – zazvorniki
    Oct 22, 2014 at 19:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.