How can I transform certain values in an object while ignoring some of the keys?
I want to loop through Objects in an array with different keys, without using a for-each loop:
[ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ]
I want to check every column mn for a length value then update it with some information, I can just do if else condition, but I think there is a better way of doing this. I tried using Object.entries
then for loop, but it won't work because of id and ip columns.
javascript
add a comment |
I want to loop through Objects in an array with different keys, without using a for-each loop:
[ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ]
I want to check every column mn for a length value then update it with some information, I can just do if else condition, but I think there is a better way of doing this. I tried using Object.entries
then for loop, but it won't work because of id and ip columns.
javascript
2
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array likemn[1], mn[2], etc
.
– Mark Meyer
Nov 20 at 19:26
have you looked atObject.keys
?
– Jhecht
Nov 20 at 19:26
You can usefor (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
what information do you want to updatemn*
keys with?
– Dacre Denny
Nov 20 at 19:30
add a comment |
I want to loop through Objects in an array with different keys, without using a for-each loop:
[ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ]
I want to check every column mn for a length value then update it with some information, I can just do if else condition, but I think there is a better way of doing this. I tried using Object.entries
then for loop, but it won't work because of id and ip columns.
javascript
I want to loop through Objects in an array with different keys, without using a for-each loop:
[ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ]
I want to check every column mn for a length value then update it with some information, I can just do if else condition, but I think there is a better way of doing this. I tried using Object.entries
then for loop, but it won't work because of id and ip columns.
javascript
javascript
edited Nov 20 at 20:47
Roy Scheffers
2,15291725
2,15291725
asked Nov 20 at 19:23
The 4th pyramid
55110
55110
2
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array likemn[1], mn[2], etc
.
– Mark Meyer
Nov 20 at 19:26
have you looked atObject.keys
?
– Jhecht
Nov 20 at 19:26
You can usefor (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
what information do you want to updatemn*
keys with?
– Dacre Denny
Nov 20 at 19:30
add a comment |
2
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array likemn[1], mn[2], etc
.
– Mark Meyer
Nov 20 at 19:26
have you looked atObject.keys
?
– Jhecht
Nov 20 at 19:26
You can usefor (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
what information do you want to updatemn*
keys with?
– Dacre Denny
Nov 20 at 19:30
2
2
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array like
mn[1], mn[2], etc
.– Mark Meyer
Nov 20 at 19:26
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array like
mn[1], mn[2], etc
.– Mark Meyer
Nov 20 at 19:26
have you looked at
Object.keys
?– Jhecht
Nov 20 at 19:26
have you looked at
Object.keys
?– Jhecht
Nov 20 at 19:26
You can use
for (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
You can use
for (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
what information do you want to update
mn*
keys with?– Dacre Denny
Nov 20 at 19:30
what information do you want to update
mn*
keys with?– Dacre Denny
Nov 20 at 19:30
add a comment |
3 Answers
3
active
oldest
votes
Perhaps something like this would work for you? This solution takes a functional approach to your problem, and avoids explict use of the for-loop
construct:
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
add a comment |
You can check the name of the key before you modify it.
Does this help? Try it out by running the code snippet.
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
add a comment |
You can use Object.entries
, Array.prototype.filter
, and Array.prototype.forEach
to your advantage.
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
This starts to get verbose if you can't use the latest ECMAScript features, though:
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
If you're stuck on a project without Babel, your best bet may be your original suggestion.
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400126%2fhow-can-i-transform-certain-values-in-an-object-while-ignoring-some-of-the-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Perhaps something like this would work for you? This solution takes a functional approach to your problem, and avoids explict use of the for-loop
construct:
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
add a comment |
Perhaps something like this would work for you? This solution takes a functional approach to your problem, and avoids explict use of the for-loop
construct:
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
add a comment |
Perhaps something like this would work for you? This solution takes a functional approach to your problem, and avoids explict use of the for-loop
construct:
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
Perhaps something like this would work for you? This solution takes a functional approach to your problem, and avoids explict use of the for-loop
construct:
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
var input = [ { id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' },
{ id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: '' } ];
var output = input.map(object => {
return Object.entries(object).reduce((result, entry) => {
let key = entry[0];
let value = entry[1];
if(key.startsWith('mn')) {
value = `updated value for ${ key }`;
}
result[key] = value;
return result;
}, {});
});
console.log(output);
answered Nov 20 at 19:36
Dacre Denny
9,7014929
9,7014929
add a comment |
add a comment |
You can check the name of the key before you modify it.
Does this help? Try it out by running the code snippet.
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
add a comment |
You can check the name of the key before you modify it.
Does this help? Try it out by running the code snippet.
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
add a comment |
You can check the name of the key before you modify it.
Does this help? Try it out by running the code snippet.
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
You can check the name of the key before you modify it.
Does this help? Try it out by running the code snippet.
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
const arr = [{
id: 1,
ip: 'xx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 2,
ip: 'xxx.xxx.xx.xx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
},
{
id: 4,
ip: 'xxx.xxx.xx.xxx',
mn1: '',
mn2: '',
mn3: '',
mn4: '',
mn5: '',
mn6: '',
mn7: ''
}
];
console.log('BEFORE', JSON.stringify(arr, null, 2));
for (const item of arr) {
for (const key of Object.keys(item)) {
if (!key.startsWith('mn')) continue;
// know if the code gets here then it's an `mn` key
// you can do logic here on the key
// and then manipulate the result
item[key] = 'changed';
}
}
console.log('AFTER', arr);
answered Nov 20 at 19:32
Rico Kahler
4,42131933
4,42131933
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
add a comment |
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
I think the goal was to avoid for loops
– Dacre Denny
Nov 20 at 19:32
add a comment |
You can use Object.entries
, Array.prototype.filter
, and Array.prototype.forEach
to your advantage.
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
This starts to get verbose if you can't use the latest ECMAScript features, though:
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
If you're stuck on a project without Babel, your best bet may be your original suggestion.
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
add a comment |
You can use Object.entries
, Array.prototype.filter
, and Array.prototype.forEach
to your advantage.
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
This starts to get verbose if you can't use the latest ECMAScript features, though:
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
If you're stuck on a project without Babel, your best bet may be your original suggestion.
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
add a comment |
You can use Object.entries
, Array.prototype.filter
, and Array.prototype.forEach
to your advantage.
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
This starts to get verbose if you can't use the latest ECMAScript features, though:
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
If you're stuck on a project without Babel, your best bet may be your original suggestion.
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
You can use Object.entries
, Array.prototype.filter
, and Array.prototype.forEach
to your advantage.
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
This starts to get verbose if you can't use the latest ECMAScript features, though:
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
If you're stuck on a project without Babel, your best bet may be your original suggestion.
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
const transform = value => `transformed-${value}`;
const arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(obj => {
Object.entries(obj)
.filter(([key]) => key !== "id" && key !== "ip")
.forEach(([key, value]) => (obj[key] = transform(value)));
});
console.log(arr)
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj)
.filter(function(keyValue) {
var key = keyValue[0];
return key !== "id" && key !== "ip";
})
.forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
obj[key] = transform(value);
});
});
console.log(arr)
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
var transform = function(value) { return "transformed-" + value };
var arr = [{ id: "id", ip: "ip", otherKey: "other" }];
arr.forEach(function(obj) {
Object.entries(obj).forEach(function(keyValue) {
var key = keyValue[0],
value = keyValue[1];
if (key !== "id" && key !== "ip") {
obj[key] = transform(value);
}
});
});
console.log(arr)
answered Nov 20 at 19:59
Michael Hines
384
384
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400126%2fhow-can-i-transform-certain-values-in-an-object-while-ignoring-some-of-the-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
When you find yourself using variable/key names ending in numbers its a good indication that you probably want an array like
mn[1], mn[2], etc
.– Mark Meyer
Nov 20 at 19:26
have you looked at
Object.keys
?– Jhecht
Nov 20 at 19:26
You can use
for (var item in obj){ .. }
– Spencer Wieczorek
Nov 20 at 19:29
what information do you want to update
mn*
keys with?– Dacre Denny
Nov 20 at 19:30