循环遍历javascript对象的最佳实践[重复]

时间:2022-01-16 21:15:04

This question already has an answer here:

这个问题在这里已有答案:

I have the following JS object which I need to apply parseFloat to any number value field (in order for ngTable to sort correctly).

我有以下JS对象,我需要将parseFloat应用于任何数字值字段(以便ngTable正确排序)。

I'm having a tough time looping through the Object to do this. I've tried nested angular.forEach but am having scoping issues (inner loops don't see outer variables).

我正在通过Object循环来完成这项工作。我已经尝试过嵌套的angular.forEach但是我遇到了范围问题(内部循环没有看到外部变量)。

What's the best manner to approach this?

接近这个的最佳方式是什么?

Edit: should have mentioned that the Object names (i.e: Person, PersonDetails) are dynamic. :/

编辑:应该提到对象名称(即:Person,PersonDetails)是动态的。 :/

My object :

我的目标:

{
    "data": [
        {
            "Person": {
                "id" : "1",
                "age": "23",
                "days": "5",
                "first_name": "Joe",
                "last_name": "Smith",
            },
            "PersonDetails": {
                "id": "4",
                "name": "Cousin",
                "oldest: "2",
            }
        },
        {
            "Person": {
                "id" : "2",
                "age": "18",
                "days": "3",
                "first_name": "John",
                "last_name": "Doe",
            },
            "PersonDetails": {
                "id": "4",
                "name": "Second Cousing",
                "oldest: "3",
            }
        }
        ...
        ...
    ]
};

1 个解决方案

#1


8  

You can do a test like this

你可以做这样的测试

function representsNumber(str) {
    return str === (+str).toString();
}

// e.g. usage
representsNumber('a'); // false
representsNumber([]); // false
representsNumber(1); // false (it IS a number)
representsNumber('1.5'); // true
representsNumber('-5.1'); // true
representsNumber('NaN'); // true

And recurse over all your nodes, overkill example

并且递归所有节点,例如,矫枉过正

function seeker(o, test, _true, _false) {
    _true || (_true = function (e) {return e;});
    _false || (_false = function (e) {return e;});
    function recursor(o) {
        var k;
        if (o instanceof Array)
            for (k = 0; k < o.length; ++k) // iterate over array
                if (typeof o[k] !== 'object')
                    o[k] = test(o[k]) ? _true(o[k]) : _false(o[k]);
                else
                    recursor(o[k]);
        else
            for (k in o) // iterate over object
                if (typeof o[k] !== 'object')
                    o[k] = test(o[k]) ? _true(o[k]) : _false(o[k]);
                else
                    recursor(o[k]);
    }
    if (typeof o === "object") return recursor(o), o;
    else return test(o) ? _true(o) : _false(o); // not an object, just transform
}

// e.g. usage
seeker({foo: [{bar: "20"}]}, representsNumber, parseFloat);
// {foo: [{bar: 20}]}

#1


8  

You can do a test like this

你可以做这样的测试

function representsNumber(str) {
    return str === (+str).toString();
}

// e.g. usage
representsNumber('a'); // false
representsNumber([]); // false
representsNumber(1); // false (it IS a number)
representsNumber('1.5'); // true
representsNumber('-5.1'); // true
representsNumber('NaN'); // true

And recurse over all your nodes, overkill example

并且递归所有节点,例如,矫枉过正

function seeker(o, test, _true, _false) {
    _true || (_true = function (e) {return e;});
    _false || (_false = function (e) {return e;});
    function recursor(o) {
        var k;
        if (o instanceof Array)
            for (k = 0; k < o.length; ++k) // iterate over array
                if (typeof o[k] !== 'object')
                    o[k] = test(o[k]) ? _true(o[k]) : _false(o[k]);
                else
                    recursor(o[k]);
        else
            for (k in o) // iterate over object
                if (typeof o[k] !== 'object')
                    o[k] = test(o[k]) ? _true(o[k]) : _false(o[k]);
                else
                    recursor(o[k]);
    }
    if (typeof o === "object") return recursor(o), o;
    else return test(o) ? _true(o) : _false(o); // not an object, just transform
}

// e.g. usage
seeker({foo: [{bar: "20"}]}, representsNumber, parseFloat);
// {foo: [{bar: 20}]}