最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to change the objects key value using lodash - Stack Overflow

programmeradmin3浏览0评论
var flagsObj = {
  logical1Flag: false,
  operator1Flag: false,
  resourceLeftFlag: false,
  resource1Flag: false,
  resourceRightFlag: false,
};

in the object above i wanted to change the value of any one of them and make it to true and the rest i wanted to make it to false. i have used this function which is mentioned below, however i am able to change the value of the using the map values or foreach function but the original flagsobj is not getting changes.

function settingFlag(val, keyF) { 
   const trueVal = val;
   const falseVal = false;
   const assignedKey = keyF;

_.mapValues(this.flagsObj, (value, key) => {
  if (key === assignedKey) {
    value = true;
  } else {
    value = false;
  }
  // console.log(value);
  console.log(`
    Flags
    ====
    key ${key} value ${value}
  `);
});

or using the forEach to manipulate the data

_.forEach(this.flagsObj, (value, key) => {
  if (key === assignedKey) {
    value = true;
    console.log(`
    flags
    =====
    key ${key} value ${value}
    `);
  } else {
    value = false;
  }
});
console.log('::::: FLAGS :::: ', flagsObj);
}

both foreach or map values are changing the required values the but the original flagsObj is not getting changed.

any lodash function which would solve the problem is appreciated. i am using angular 1.5 es6 application

var flagsObj = {
  logical1Flag: false,
  operator1Flag: false,
  resourceLeftFlag: false,
  resource1Flag: false,
  resourceRightFlag: false,
};

in the object above i wanted to change the value of any one of them and make it to true and the rest i wanted to make it to false. i have used this function which is mentioned below, however i am able to change the value of the using the map values or foreach function but the original flagsobj is not getting changes.

function settingFlag(val, keyF) { 
   const trueVal = val;
   const falseVal = false;
   const assignedKey = keyF;

_.mapValues(this.flagsObj, (value, key) => {
  if (key === assignedKey) {
    value = true;
  } else {
    value = false;
  }
  // console.log(value);
  console.log(`
    Flags
    ====
    key ${key} value ${value}
  `);
});

or using the forEach to manipulate the data

_.forEach(this.flagsObj, (value, key) => {
  if (key === assignedKey) {
    value = true;
    console.log(`
    flags
    =====
    key ${key} value ${value}
    `);
  } else {
    value = false;
  }
});
console.log('::::: FLAGS :::: ', flagsObj);
}

both foreach or map values are changing the required values the but the original flagsObj is not getting changed.

any lodash function which would solve the problem is appreciated. i am using angular 1.5 es6 application

Share Improve this question asked Jul 13, 2017 at 19:14 Swapnil SrivastavaSwapnil Srivastava 1732 silver badges13 bronze badges 1
  • mapValues returns a new object. Something like let result = _.mapValues(this.flagsObj, (v, k) => key === assignedKey) will do what you want. – Gruff Bunny Commented Jul 13, 2017 at 21:05
Add a ment  | 

6 Answers 6

Reset to default 4

Not sure about lodash, but a one liner in plain js will do it

var flagsObj = {
  logical1Flag: false,
  operator1Flag: false,
  resourceLeftFlag: false,
  resource1Flag: false,
  resourceRightFlag: false,
};

function change(obj, key) {
  return Object.keys(obj).forEach(v => obj[v] = v === key);
}

change(flagsObj, "logical1Flag");

console.log(flagsObj);

One way to solve this is to store the original flagsObj as a constant or reset object that you can use to restore the false of all flags. Assign this to the flagsObj, make sure to clone it using lodash#clone to avoid mutating the ORIGINAL_FLAG_OBJECT variable. Next is to create a function that accepts a flag that we set to true; we acplish this by using lodash#assign to assign all values from the ORIGINAL_FLAG_OBJECT to the flagsObj to modify all flags to false, and then use lodash#set to set that particular flag key to true.

var ORIGINAL_FLAG_OBJECT = {
  logical1Flag: false,
  operator1Flag: false,
  resourceLeftFlag: false,
  resource1Flag: false,
  resourceRightFlag: false,
};

var flagsObj = _.clone(ORIGINAL_FLAG_OBJECT);

function activateFlag(flag) {
  return _(flagsObj)
    .assign(ORIGINAL_FLAG_OBJECT)
    .set(flag, true)
    .value();
}

console.log(activateFlag('resourceRightFlag'));
console.log(activateFlag('resource1Flag'));
console.log(activateFlag('resourceLeftFlag'));
console.log(activateFlag('operator1Flag'));
console.log(activateFlag('logical1Flag'));
.as-console-wrapper { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

You need to return it:

function settingFlag(val, keyF) { 
   const trueVal = val;
   const falseVal = false;
   const assignedKey = keyF;

  return _.mapValues(this.flagsObj, (value, key) => {
    if (key === assignedKey) {
      value = true;
    } else {
      value = false;
    }
    return value;
  });
}

According to lodash docs:

Returns the new mapped array.

It doesn't mutate the original one.

With you _.forEach one, first off, you should only use it on an array (you're using it on an object). It might technically work, and for the sake of argument, even if you do use forEach, setting value = true only sets a variable, not the property of an object. You'd need to do this.flagsObject[key] = // true or false.

Depending on what you're trying to do, there might be an easier way. If only one of the flags can be true at a time, and you're just trying to set which one;

this.flags = ['logical1', 'operator1', 'resourceLeft', 'resource1', 'resourceRight'];
this.currentFlag = -1;

then, to set the current flag, do this.currentFlag = this.flags.findIndex(a => a === keyF), and to get the currently set flag, do this.flags[this.currentFlag]. This is more efficient than iterating the object each time, and stays nice and simple.

You can iterate through the keys of object using a simple for and set the value to the key that matches with the one specified, and false otherwise:

var flagsObj = {
  logical1Flag: false,
  operator1Flag: false,
  resourceLeftFlag: false,
  resource1Flag: false,
  resourceRightFlag: false,
};

function settingFlag(val, keyF) {
  for (var key in flagsObj) {
    flagsObj[key] = (key === keyF) ? val : false;
  }
}

settingFlag(true, 'operator1Flag');

console.log(flagsObj);

Easy; use the obj param of forEach

_.forEach(this.flagsObj, (value, key, obj) => {
  obj[key] = key === assignedKey
});
发布评论

评论列表(0)

  1. 暂无评论