How do I use nodejs to structure a json file outputted from a nodejs script?
up vote
0
down vote
favorite
I am using nodejs script to translate a json file from English to French. The translations are being outputted into a fr.json file but not in the format I need.
This is the input en.json file that I want to translate:
[
{
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email was not found, please register here",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
{
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Already have an account?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
]
This is the output fr.json format I need, (just the values of the id and defaultMessage):
{
"EnterEmailForm.pleaseRegisterHere": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"EnterEmailForm.alreadyHaveAnAccount": "Vous avez déjà un compte?",
}
This is the output fr.json that the script gives me:
{
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
}
This is the script that translates the en.json file to a fr.json file:
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
How do I edit the script to correctly format the outputted file fr.json to the one I need?
javascript node.js
add a comment |
up vote
0
down vote
favorite
I am using nodejs script to translate a json file from English to French. The translations are being outputted into a fr.json file but not in the format I need.
This is the input en.json file that I want to translate:
[
{
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email was not found, please register here",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
{
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Already have an account?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
]
This is the output fr.json format I need, (just the values of the id and defaultMessage):
{
"EnterEmailForm.pleaseRegisterHere": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"EnterEmailForm.alreadyHaveAnAccount": "Vous avez déjà un compte?",
}
This is the output fr.json that the script gives me:
{
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
}
This is the script that translates the en.json file to a fr.json file:
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
How do I edit the script to correctly format the outputted file fr.json to the one I need?
javascript node.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using nodejs script to translate a json file from English to French. The translations are being outputted into a fr.json file but not in the format I need.
This is the input en.json file that I want to translate:
[
{
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email was not found, please register here",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
{
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Already have an account?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
]
This is the output fr.json format I need, (just the values of the id and defaultMessage):
{
"EnterEmailForm.pleaseRegisterHere": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"EnterEmailForm.alreadyHaveAnAccount": "Vous avez déjà un compte?",
}
This is the output fr.json that the script gives me:
{
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
}
This is the script that translates the en.json file to a fr.json file:
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
How do I edit the script to correctly format the outputted file fr.json to the one I need?
javascript node.js
I am using nodejs script to translate a json file from English to French. The translations are being outputted into a fr.json file but not in the format I need.
This is the input en.json file that I want to translate:
[
{
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email was not found, please register here",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
{
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Already have an account?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
]
This is the output fr.json format I need, (just the values of the id and defaultMessage):
{
"EnterEmailForm.pleaseRegisterHere": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"EnterEmailForm.alreadyHaveAnAccount": "Vous avez déjà un compte?",
}
This is the output fr.json that the script gives me:
{
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
}
This is the script that translates the en.json file to a fr.json file:
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
How do I edit the script to correctly format the outputted file fr.json to the one I need?
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
#!/usr/bin/env node
const fs = require('fs');
const moment = require('moment');
const _ = require('lodash');
const path = require('path');
const agent = require('superagent-promise')(require('superagent'), Promise);
//Lang Codes https://ctrlq.org/code/19899-google-translate-languages
if (process.argv.length >= 5) {
//Args
const apiKey = process.argv[2];
const inputFile = process.argv[3];
const destinationCodes = process.argv[4].split(',');
const apiUrl = _.template('https://www.googleapis.com/language/translate/v2?key=<%= apiKey %>&q=<%= value %>&source=en&target=<%= languageKey %>');
function transformResponse(res) {
return _.get(JSON.parse(res.text), [ 'data', 'translations', 0, 'translatedText' ]);
}
function iterLeaves(value, keyChain, accumulator, languageKey) {
accumulator = accumulator || {};
keyChain = keyChain || ;
if (_.isObject(value)) {
return _.chain(value).reduce((handlers, v, k) => {
return handlers.concat(iterLeaves(v, keyChain.concat(k), accumulator, languageKey));
}, ).flattenDeep().value();
} else {
return function () {
console.log(_.template('Translating <%= value %> to <%= languageKey %>')({value, languageKey}));
//Translates individual string to language code
return agent('GET', apiUrl({
value: encodeURI(value),
languageKey,
apiKey
})).then(transformResponse).then((text) => {
//Sets the value in the accumulator
_.set(accumulator, keyChain, text, );
//This needs to be returned to it's eventually written to json
return accumulator;
});
};
}
}
Promise.all(_.reduce(destinationCodes, (sum, languageKey) => {
const fileName = _.template('/tmp/<%= languageKey %>.json')({
languageKey,
});
//Starts with the top level strings
return sum.concat(_.reduce(iterLeaves(JSON.parse(fs.readFileSync(path.resolve(inputFile), 'utf-8')), undefined, undefined, languageKey), (promiseChain, fn) => {
return promiseChain.then(fn);
}, Promise.resolve()).then((payload) => {
fs.writeFileSync(fileName, JSON.stringify(payload, null, 2));
}).then(_.partial(console.log, 'Successfully translated all nodes, file output at ' + fileName)));
}, )).then(() => {
process.exit();
});
} else {
console.error('You must provide an input json file and a comma-separated list of destination language codes.');
}
javascript node.js
javascript node.js
edited Nov 12 at 2:52
Alexander
2,99261327
2,99261327
asked Nov 12 at 2:39
Alan Judi
26011
26011
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Once you have the object that's produced currently, you can transform it into your desired output by reduce
ing the object's values into another object, whose keys are the id
s and whose values are the defaultMessage
s:
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
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',
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%2f53255326%2fhow-do-i-use-nodejs-to-structure-a-json-file-outputted-from-a-nodejs-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Once you have the object that's produced currently, you can transform it into your desired output by reduce
ing the object's values into another object, whose keys are the id
s and whose values are the defaultMessage
s:
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
add a comment |
up vote
2
down vote
accepted
Once you have the object that's produced currently, you can transform it into your desired output by reduce
ing the object's values into another object, whose keys are the id
s and whose values are the defaultMessage
s:
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Once you have the object that's produced currently, you can transform it into your desired output by reduce
ing the object's values into another object, whose keys are the id
s and whose values are the defaultMessage
s:
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
Once you have the object that's produced currently, you can transform it into your desired output by reduce
ing the object's values into another object, whose keys are the id
s and whose values are the defaultMessage
s:
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
const payload = {
"0": {
"id": "EnterEmailForm.pleaseRegisterHere",
"defaultMessage": "Email n'a pas été trouvé, veuillez vous inscrire ici",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
},
"1": {
"id": "EnterEmailForm.alreadyHaveAnAccount",
"defaultMessage": "Vous avez déjà un compte?",
"filepath": "./src/accounts/components/EnterEmailForm/EnterEmailForm.js"
}
};
const output = Object.values(payload).reduce((a, { id, defaultMessage }) => {
a[id] = defaultMessage;
return a;
}, {});
console.log(output);
answered Nov 12 at 2:45
CertainPerformance
71.3k143453
71.3k143453
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%2f53255326%2fhow-do-i-use-nodejs-to-structure-a-json-file-outputted-from-a-nodejs-script%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