简体   繁体   中英

Picking and choosing items from an object

So I have an object that I am looping through but, it outputs a whole bunch of stuff in it that I need but a lot of it I also do not need for the page I am designing. How can I remove or pull just the stuff I need by key? Or is there an easier way?

  objectToText: function(obj) {
        var text = ""
        for (var key in obj) {
            text += "<b>" + key + " (" + typeof obj[key] + "):</b>  ";
            if (typeof obj[key] == "object") {
                text += "[<br>";
                for (var i = 0; i < obj[key].length; ++i) {
                    text += this.objectToText(obj[key][i]);
                }
                text += "<br>]<br>";
            } else {
                text += obj[key] + "<br>";
            }
        }
        return text;
    }

You could alter your method like this (note I modified the html output to provide nesting, but that isn't strictly necessary): http://jsfiddle.net/Shmiddty/QUXgE/

function objectToText(obj, keys) {
    if (obj == undefined) return "";

    var keys = keys || ['length', 'width', 'height', 'banana'];
    var text = "";
    var key;
    text += "<div>";
    for (var i = 0; i < keys.length; i++) {

        key = keys[i];
        if (key in obj) {
            text += key + " (" + typeof obj[key] + "):";
            if (typeof obj[key] == "object") {
                text += "<div>{" + objectToText(obj[key], keys) + "}</div>";
            } else {
                text += "&nbsp;" + obj[key] + "<br>";
            }
        }

    }
    text += "</div>"; 
    return text;
}

And it could be used like this:

var obj = {
    length: 100,
    width: {
        length: 100,
        width: 50,
        nestedSecret: 0,
        banana:{
            length: 50,
            width:25,
            ultraSecret:42
        }
    },
    height: 50,
    superSecret: '007'
};
var keys = ["superSecret", "width", "nestedSecret", "banana", "ultraSecret"];
document.getElementById('output').innerHTML = objectToText(obj);        // use default keys
document.getElementById('output2').innerHTML = objectToText(obj, keys);​ // use custom keys

I don't know what exactly you're looking for, but sure you can access by key. For example:

var obj = {
    "something" : 1,
    "something_else" : 2,
    "some_array" : [10, 20, 30],
    "nested_obj" : { "thekey" : 100 }
}
console.log(obj.something); // 1;
console.log(obj.something_else); // 2;
console.log(obj.some_array[0]); // 10;
console.log(obj.nested_obj.thekey); // 100;

// Or:

console.log(obj['something']); // 1;
console.log(obj['something_else']); // 2;
console.log(obj['some_array'][0]); // 10;
console.log(obj['nested_obj']['thekey']); // 100;

Or, if by "stuff you don't need" you mean inherited properties/methods of your object, you can check that with Object.hasOwnProperty .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM