1

I am just getting started with this so I am confused about how I would work with the following.

// so I believe this really isn't an object just a string representation, but for example please excuse the name
var dataObj = "{'id': 1, 
                'data': { 'color': 'red', 
                          'shape' : 'triangle', 
                          'height' : 100, 
                          'width' : 45, 
                           },
                'id': 2, 
                'data': { 'color': 'blue', 
                          'shape' : 'square', 
                          'height' : 75, 
                          'width' : 67, 
                          },
                'id': 3, 
                'data': { 'color': 'orange', 
                          'shape' : 'circle', 
                          'height' : 89, 
                          'width' :24, 
                          }
                }";

so what I am having problems with is how would I update specific subset of the data values by the id (like a SQL UPDATE WHERE kind of thing)? javascript or jquery really doesn't matter to me, just don't know an approach for either.

dataObjUpdate(2);    
function dataObjUpdate (passedID) {

    //access the data by the passedID match and update the color to black
}

appreciate the help guys....

3
  • JSON is a data serialization format (your object in a string in your question is not valid JSON by the way, JSON uses double quotes). You can remove the quotes and you'll get a valid JavaScript object Commented Jul 5, 2013 at 12:40
  • 1
    The more important problem with your data format is, that the id property occurs multiple times in the serialized object. I think you might want to change to an array of objects, instead of this one object. Commented Jul 5, 2013 at 12:42
  • yeah that makes sense....bad example fail :). thanks Commented Jul 5, 2013 at 12:44

1 Answer 1

2

If we ignore the comment I left and assume you have a JavaScript object. I see the following problems:

  • Your ids are outside of your nested object.
  • You're using an object but you want a 'list' you can use an array for that.

Here is how I'd structure the object myself:

var data = [{ 
        color : 'red', 
        shape : 'triangle', 
        height : 100, 
        width : 45, 
        id:1
    },
    { 
        color: 'blue', 
        shape : 'square', 
        height : 75, 
        width : 67, 
        id: 2
    },
    {
        color: 'orange', 
        shape : 'circle', 
        height : 89, 
        width :24,
        id :3 
    }];

Now, I can query it like you'd expect using filter:

var id3 = data.filter(function(elem){
             return elem.id === 3;
          })[0];
   id3;//the third object, with id 3

ES6 has a method called find which would save the [0] at the end (which means the first element)

var id3 = data.find(function(elem){
             return elem.id === 3;
          });
   id3;//the third object, with id 3

Or, you can use a simple for loop

var id3 = (function find(arr){
              for(var i=0;i<arr.length;i++){
                  if(arr[i].id === 3){
                      return arr[i];
                  }
              }
          })(data);
id3;
Sign up to request clarification or add additional context in comments.

7 Comments

A dictionary keyed by id also makes (and is more performant). That is if id's are unique
Yes, that's a valid point, indexing IDs in another object (or map) could be very beneficial. If you're storing a lot of data and only support modern browser, you should also consider IndexedDB. Not to mention that if IDs are sequential you can access elements through the array index.
thanks Benjamin! i appreciate the help/direction! I don't quite understand the method for indexing, but I suppose that is another question :). appreciated!
@Justin You're welcome. Please consider accepting this answer if it solves your issue. Also, I suggest the JavaScript objects/arrays chapter in eloquent JavaScript or the part on objects in mdn, it explains it nicely. Good luck!
figured out my mistake btw...examples work great! once again..thanks!
|

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.