简体   繁体   English

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

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

I have an array that at any point may contain any combination of the following values: 我有一个数组,随时可能包含以下值的任意组合:

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

The goal is to rebuild a Javascript Object that, by default, is set to: 目标是重建默认情况下设置为的Javascript对象:

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

The positions array is looped through to rebuild the currentPositioning Object: 循环定位数组以重建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') {
        ...

Basically, if one of the positions is in the positions array, then that position in the currentPositioning Object should be set to true ..otherwise it should remain false 基本上,如果位置之一在positions数组中,则currentPositioning Object中的该位置设置为true ..否则,它应保持为false

The idea is that..when.. 这个想法是..什么时候..

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

..then.. ..然后..

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

For some reason though, right now the index = -1 .. every time .. so the result keeps turning to something like this!? 但是由于某种原因,现在每次index = -1 ..因此结果不断转向这样! :

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

You can use Underscore.js to do this (I've added some temp variables to improve readibility): 您可以使用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