繁体   English   中英

替换javascript对象中的字段? 与拼接和推,拼接的indexOf返回-1值?

[英]Replace a field in a javascript Object? with splice and push, indexOf to splice is returning -1 value?

我有一个数组,随时可能包含以下值的任意组合:

var positions = ['first', 'second', 'third', 'fourth'];

目标是重建默认情况下设置为的Javascript对象:

currentPositioning = { 'positioning': [
                                      { 'first': false },
                                      { 'second': false },
                                      { 'third': false },
                                      { 'fourth': false } 
                                    ]
                                };

循环定位数组以重建currentPositioning对象:

positions.forEach(setPositions);
function setPositions(element, index, array) {
    if (element == 'first') {
        // define objSplice object
        var objSplice = {};
        // set object of 'array.element' to false .. {'element': false}
        objSplice[element] = false;
        console.log('objSplice = ' + JSON.stringify(objSplice));
        // find index that matches {'element': false}
        var index = currentPositioning["positioning"].indexOf( objSplice );
        console.log('index = ' + index);
        if (index > -1) {
                // remove index that matches {'element': false}
            currentPositioning["positioning"].splice(index, 1);
        }
        // define obj object
        var obj = {};
        // set obj object of 'array.element' to true .. {'element': true}
        obj[element] = true;
        // add {'element': true} to array
        currentPositioning["positioning"].push( obj );
    }
    if (element == 'second') {
        ...

基本上,如果位置之一在positions数组中,则currentPositioning Object中的该位置设置为true ..否则,它应保持为false

这个想法是..什么时候..

var positions = ['first', 'second', 'third'];

..然后..

currentPositioning = { 'positioning': [
                                      { 'first': true },
                                      { 'second': true },
                                      { 'third': true },
                                      { 'fourth': false } 
                                    ]
                                };

但是由于某种原因,现在每次index = -1 ..因此结果不断转向这样!

currentPositioning = { 'positioning': [
                                      { 'first': false },
                                      { 'second': false },
                                      { 'third': false },
                                      { 'fourth': false },
                                      { 'first': true },
                                      { 'second': true },
                                      { 'third': true },
                                    ]
                                };

您可以使用Underscore.js做到这一点(我已经添加了一些临时变量,以提高可读性):

var positions = ['first', 'second', 'third'];

var updatedPositions = _.map(['first', 'second', 'third', 'fourth'], function(p) {
  var json={};
  json[p] = _.contains(positions,p);
  return json;
});

var currentPositioning = { 'positioning': [
  updatedPositions
]
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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