I want use dynamic {i} in loop for access obj array after parsing like modele_{i}.view_[i] for access values
please help me to solve my problem
how can replace this line
var Ob = obj.permissions[i-1].module_1.view_1;
With
var Ob = obj.permissions[i-1].module_{i}.view_{i};
const data = '{"permissions": [{"module_1": {"view_1":"false","edit_1":"false","delete_1":"false","add_1":"false","search_1":"false"}},{"module_2": {"view_2":"false","edit_2":"false","delete_2":"false","add_2":"false","search_2":"false"}},{"module_3": {"view_3":"false","edit_3":"false","delete_3":"false","add_3":"false","search_3":"false"}},{"module_4": {"view_4":"false","edit_4":"false","delete_4":"false","add_4":"false","search_4":"false"}},{"module_5": {"view_5":"false","edit_5":"false","delete_5":"false","add_5":"false","search_5":"false"}},{"module_6": {"view_6":"false","edit_6":"false","delete_6":"false","add_6":"false","search_6":"false"}},{"module_7": {"view_7":"false","edit_7":"false","delete_7":"false","add_7":"false","search_7":"false"}},{"module_8": {"view_8":"false","edit_8":"false","delete_8":"false","add_8":"false","search_8":"false"}},{"module_9": {"view_9":"false","edit_9":"false","delete_9":"false","add_9":"false","search_9":"false"}},{"module_10": {"view_10":"false","edit_10":"false","delete_10":"false","add_10":"false","search_10":"false"}},{"module_11": {"view_11":"false","edit_11":"false","delete_11":"false","add_11":"false","search_11":"false"}},{"module_12": {"view_12":"false","edit_12":"false","delete_12":"false","add_12":"false","search_12":"false"}},{"module_13": {"view_13":"false","edit_13":"false","delete_13":"false","add_13":"false","search_13":"false"}},{"module_14": {"view_14":"false","edit_14":"false","delete_14":"false","add_14":"false","search_14":"false"}},{"module_15": {"view_15":"false","edit_15":"false","delete_15":"false","add_15":"false","search_15":"false"}},{"module_16": {"view_16":"false","edit_16":"false","delete_16":"false","add_16":"false","search_16":"false"}},{"module_17": {"view_17":"false","edit_17":"false","delete_17":"false","add_17":"false","search_17":"false"}},{"module_18": {"view_18":"false","edit_18":"false","delete_18":"false","add_18":"false","search_18":"false"}},{"module_19": {"view_19":"false","edit_19":"false","delete_19":"false","add_19":"false","search_19":"false"}},{"module_20": {"view_20":"false","edit_20":"false","delete_20":"false","add_20":"false","search_20":"false"}},{"module_21": {"view_21":"false","edit_21":"false","delete_21":"false","add_21":"false","search_21":"false"}},{"module_22": {"view_22":"false","edit_22":"false","delete_22":"false","add_22":"false","search_22":"false"}},{"module_23": {"view_23":"false","edit_23":"false","delete_23":"false","add_23":"false","search_23":"false"}},{"module_24": {"view_24":"false","edit_24":"false","delete_24":"false","add_24":"false","search_24":"false"}},{"module_25": {"view_25":"false","edit_25":"false","delete_25":"false","add_25":"false","search_25":"false"}},{"module_26": {"view_26":"false","edit_26":"false","delete_26":"false","add_26":"false","search_26":"false"}},{"module_27": {"view_27":"false","edit_27":"false","delete_27":"false","add_27":"false","search_27":"false"}},{"module_28": {"view_28":"false","edit_28":"false","delete_28":"false","add_28":"false","search_28":"false"}},{"module_29": {"view_29":"false","edit_29":"false","delete_29":"false","add_29":"false","search_29":"false"}},{"module_30": {"view_30":"false","edit_30":"false","delete_30":"false","add_30":"false","search_30":"false"}},{"module_31": {"view_31":"false","edit_31":"false","delete_31":"false","add_31":"false","search_31":"false"}},{"module_32": {"view_32":"false","edit_32":"false","delete_32":"false","add_32":"false","search_32":"false"}},{"module_33": {"view_33":"false","edit_33":"false","delete_33":"false","add_33":"false","search_33":"false"}},{"module_34": {"view_34":"false","edit_34":"false","delete_34":"false","add_34":"false","search_34":"false"}},{"module_35": {"view_35":"false","edit_35":"false","delete_35":"false","add_35":"false","search_35":"false"}},{"module_36": {"view_36":"false","edit_36":"false","delete_36":"false","add_36":"false","search_36":"false"}},{"module_37": {"view_37":"false","edit_37":"false","delete_37":"false","add_37":"false","search_37":"false"}},{"module_38": {"view_38":"false","edit_38":"false","delete_38":"false","add_38":"false","search_38":"false"}},{"module_39": {"view_39":"false","edit_39":"false","delete_39":"false","add_39":"false","search_39":"false"}},{"module_40": {"view_40":"false","edit_40":"false","delete_40":"false","add_40":"false","search_40":"false"}},{"module_41": {"view_41":"false","edit_41":"false","delete_41":"false","add_41":"false","search_41":"false"}},{"module_42": {"view_42":"false","edit_42":"false","delete_42":"false","add_42":"false","search_42":"false"}},{"module_43": {"view_43":"false","edit_43":"false","delete_43":"false","add_43":"false","search_43":"false"}},{"module_44": {"view_44":"false","edit_44":"false","delete_44":"false","add_44":"false","search_44":"false"}},{"module_45": {"view_45":"false","edit_45":"false","delete_45":"false","add_45":"false","search_45":"false"}},{"module_46": {"view_46":"false","edit_46":"false","delete_46":"false","add_46":"false","search_46":"false"}},{"module_47": {"view_47":"false","edit_47":"false","delete_47":"false","add_47":"false","search_47":"false"}},{"module_48": {"view_48":"false","edit_48":"false","delete_48":"false","add_48":"false","search_48":"false"}},{"module_49": {"view_49":"false","edit_49":"false","delete_49":"false","add_49":"false","search_49":"false"}},{"module_50": {"view_50":"false","edit_50":"false","delete_50":"false","add_50":"false","search_50":"false"}},{"module_51": {"view_51":"false","edit_51":"false","delete_51":"false","add_51":"false","search_51":"false"}},{"module_52": {"view_52":"false","edit_52":"false","delete_52":"false","add_52":"false","search_52":"false"}},{"module_53": {"view_53":"false","edit_53":"false","delete_53":"false","add_53":"false","search_53":"false"}},{"module_54": {"view_54":"false","edit_54":"false","delete_54":"false","add_54":"false","search_54":"false"}}],"count":"54"}';
const obj = JSON.parse(data);
let Size = obj.count;
for (let i=1; i <= Size ; i++) {
var Ob = obj.permissions[i-1].module_1.view_1;
console.log(Ob);
}
Since you only have one object that matches you don't need a loop. Just find the first object that matches.
const data = '{"permissions": [{"module_1": {"view_1":"false","edit_1":"false","delete_1":"false","add_1":"false","search_1":"false"}},{"module_2": {"view_2":"false","edit_2":"false","delete_2":"false","add_2":"false","search_2":"false"}},{"module_3": {"view_3":"false","edit_3":"false","delete_3":"false","add_3":"false","search_3":"false"}},{"module_4": {"view_4":"false","edit_4":"false","delete_4":"false","add_4":"false","search_4":"false"}},{"module_5": {"view_5":"false","edit_5":"false","delete_5":"false","add_5":"false","search_5":"false"}},{"module_6": {"view_6":"false","edit_6":"false","delete_6":"false","add_6":"false","search_6":"false"}},{"module_7": {"view_7":"false","edit_7":"false","delete_7":"false","add_7":"false","search_7":"false"}},{"module_8": {"view_8":"false","edit_8":"false","delete_8":"false","add_8":"false","search_8":"false"}},{"module_9": {"view_9":"false","edit_9":"false","delete_9":"false","add_9":"false","search_9":"false"}},{"module_10": {"view_10":"false","edit_10":"false","delete_10":"false","add_10":"false","search_10":"false"}},{"module_11": {"view_11":"false","edit_11":"false","delete_11":"false","add_11":"false","search_11":"false"}},{"module_12": {"view_12":"false","edit_12":"false","delete_12":"false","add_12":"false","search_12":"false"}},{"module_13": {"view_13":"false","edit_13":"false","delete_13":"false","add_13":"false","search_13":"false"}},{"module_14": {"view_14":"false","edit_14":"false","delete_14":"false","add_14":"false","search_14":"false"}},{"module_15": {"view_15":"false","edit_15":"false","delete_15":"false","add_15":"false","search_15":"false"}},{"module_16": {"view_16":"false","edit_16":"false","delete_16":"false","add_16":"false","search_16":"false"}},{"module_17": {"view_17":"false","edit_17":"false","delete_17":"false","add_17":"false","search_17":"false"}},{"module_18": {"view_18":"false","edit_18":"false","delete_18":"false","add_18":"false","search_18":"false"}},{"module_19": {"view_19":"false","edit_19":"false","delete_19":"false","add_19":"false","search_19":"false"}},{"module_20": {"view_20":"false","edit_20":"false","delete_20":"false","add_20":"false","search_20":"false"}},{"module_21": {"view_21":"false","edit_21":"false","delete_21":"false","add_21":"false","search_21":"false"}},{"module_22": {"view_22":"false","edit_22":"false","delete_22":"false","add_22":"false","search_22":"false"}},{"module_23": {"view_23":"false","edit_23":"false","delete_23":"false","add_23":"false","search_23":"false"}},{"module_24": {"view_24":"false","edit_24":"false","delete_24":"false","add_24":"false","search_24":"false"}},{"module_25": {"view_25":"false","edit_25":"false","delete_25":"false","add_25":"false","search_25":"false"}},{"module_26": {"view_26":"false","edit_26":"false","delete_26":"false","add_26":"false","search_26":"false"}},{"module_27": {"view_27":"false","edit_27":"false","delete_27":"false","add_27":"false","search_27":"false"}},{"module_28": {"view_28":"false","edit_28":"false","delete_28":"false","add_28":"false","search_28":"false"}},{"module_29": {"view_29":"false","edit_29":"false","delete_29":"false","add_29":"false","search_29":"false"}},{"module_30": {"view_30":"false","edit_30":"false","delete_30":"false","add_30":"false","search_30":"false"}},{"module_31": {"view_31":"false","edit_31":"false","delete_31":"false","add_31":"false","search_31":"false"}},{"module_32": {"view_32":"false","edit_32":"false","delete_32":"false","add_32":"false","search_32":"false"}},{"module_33": {"view_33":"false","edit_33":"false","delete_33":"false","add_33":"false","search_33":"false"}},{"module_34": {"view_34":"false","edit_34":"false","delete_34":"false","add_34":"false","search_34":"false"}},{"module_35": {"view_35":"false","edit_35":"false","delete_35":"false","add_35":"false","search_35":"false"}},{"module_36": {"view_36":"false","edit_36":"false","delete_36":"false","add_36":"false","search_36":"false"}},{"module_37": {"view_37":"false","edit_37":"false","delete_37":"false","add_37":"false","search_37":"false"}},{"module_38": {"view_38":"false","edit_38":"false","delete_38":"false","add_38":"false","search_38":"false"}},{"module_39": {"view_39":"false","edit_39":"false","delete_39":"false","add_39":"false","search_39":"false"}},{"module_40": {"view_40":"false","edit_40":"false","delete_40":"false","add_40":"false","search_40":"false"}},{"module_41": {"view_41":"false","edit_41":"false","delete_41":"false","add_41":"false","search_41":"false"}},{"module_42": {"view_42":"false","edit_42":"false","delete_42":"false","add_42":"false","search_42":"false"}},{"module_43": {"view_43":"false","edit_43":"false","delete_43":"false","add_43":"false","search_43":"false"}},{"module_44": {"view_44":"false","edit_44":"false","delete_44":"false","add_44":"false","search_44":"false"}},{"module_45": {"view_45":"false","edit_45":"false","delete_45":"false","add_45":"false","search_45":"false"}},{"module_46": {"view_46":"false","edit_46":"false","delete_46":"false","add_46":"false","search_46":"false"}},{"module_47": {"view_47":"false","edit_47":"false","delete_47":"false","add_47":"false","search_47":"false"}},{"module_48": {"view_48":"false","edit_48":"false","delete_48":"false","add_48":"false","search_48":"false"}},{"module_49": {"view_49":"false","edit_49":"false","delete_49":"false","add_49":"false","search_49":"false"}},{"module_50": {"view_50":"false","edit_50":"false","delete_50":"false","add_50":"false","search_50":"false"}},{"module_51": {"view_51":"false","edit_51":"false","delete_51":"false","add_51":"false","search_51":"false"}},{"module_52": {"view_52":"false","edit_52":"false","delete_52":"false","add_52":"false","search_52":"false"}},{"module_53": {"view_53":"false","edit_53":"false","delete_53":"false","add_53":"false","search_53":"false"}},{"module_54": {"view_54":"false","edit_54":"false","delete_54":"false","add_54":"false","search_54":"false"}}],"count":"54"}';
const obj = JSON.parse(data);
const number = 1;
const ob = obj.permissions.find(obj => {
return obj[`module_${number}`][`view_${number}`];
});
console.log(ob);
Related
From a txt file with values but not keys I want to add those values to a empty json file. I have to add the key for each value, save the json file and export the json file to a new txt, this time with keys.
So I have this json file
{ "agenda" : []
}
I have this txt file (all fake data in case you wonder):
"Fidel","Oltra","fidel#gmail.com","6650403234"
"Merxe","Sanz","merxe#gmail.com","65345235"
"David","Garcia","dgarcia#gmail.com","69823422"
"Amparo","López","alopez#gmail.com","67234234"
"Antonio","Gómez","antoniog#gmail.com","69929292"
And I want the json file to look like
{
"agenda":[
{
"Name": "Fidel",
"Surname": "Oltra",
"Email": "fidel#gmail.com",
"Phone": 6650403234
},
{
...
},
...
]
}
I have this code that is kind of working but I don't know how to push properly the data because at the final json file doesn't look as expected.
const archivo = require("fs");
const json = require("fs");
const file = archivo.readFileSync('agenda.txt', 'utf8');
console.log(file);
const lines = file.split('\n');
console.log(lines);
let campos;
let rawdata = json.readFileSync('agenda.json');
let json1 = JSON.parse(rawdata);
console.log(json1);
for (i in lines) {
campos = lines[i].split(",");
json1.agenda.push('Nombre:', campos[0]);
json1.agenda.push('Apellido:', campos[1]);
json1.agenda.push('Email:', campos[2]);
json1.agenda.push('Teléfono:', campos[3]);
console.log(campos);
console.log(json1);
};
let data = JSON.stringify(json1);
json.writeFileSync('agenda2.json', data);
And the json when I open it is:
{"agenda":["Nombre:","\"Fidel\"","Apellido:","\"Oltra\"","Email:","\"fidel#gmail.com\"","Teléfono:","\"6650403234\"\r","Nombre:","\"Merxe\"","Apellido:","\"Sanz\"","Email:","\"merxe#gmail.com\"","Teléfono:","\"65345235\"\r","Nombre:","\"David\"","Apellido:","\"Garcia\"","Email:","\"dgarcia#gmail.com\"","Teléfono:","\"69823422\"\r","Nombre:","\"Amparo\"","Apellido:","\"López\"","Email:","\"alopez#gmail.com\"","Teléfono:","\"67234234\"\r","Nombre:","\"Antonio\"","Apellido:","\"Gómez\"","Email:","\"antoniog#gmail.com\"","Teléfono:","\"69929292\""]}
So I would like some help to make it work and learn what I am doing wrong... and also to know how to write the right final json back to txt file.
There's two issues I found.
You never removed the quotes / cotizaciones from agenta.txt. This means that your ending json was escaping the quotes for you. "String" in JSON is "\"String\"".
In the for loop, you were pushing your keys and values into an array instead of making an object, and then pushing that object in the array.
const fs = require("fs");
const file = fs.readFileSync("agenda.txt", "utf8");
console.log(file);
const sinCotiz = file.replace(/"/g, ""); // remove quotes
const lines = sinCotiz.split("\n");
console.log(lines);
const rawdata = fs.readFileSync("agenda.json");
const json1 = JSON.parse(rawdata);
console.log(json1);
for (i in lines) {
const obj = {}; // create an object
const campos = lines[i].split(",");
obj["Nombre"] = campos[0]; // create keys and values for the object
obj["Apellido"] = campos[1];
obj["Email"] = campos[2];
obj["Teléfono"] = campos[3];
json1.agenda.push(obj); // push the entire object in the array
console.log(campos);
console.log(json1);
}
const data = JSON.stringify(json1);
fs.writeFileSync("agenda2.json", data);
i think there is more simple solution,
but you can format your file as it is a CSV file . you will put a layer at first to remove the '' and change , with ; , then you will need to add a header line in your case the header will be
Nombre;Apellido;Email;Teléfono
then you use this function :
function csvJSON(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}
this will gives you a formated json with the following strecture :
[{Nombre:4444,Apellido:"AAAA",....}]
and you can put the whole function directly to your agenda :)
again i think there is more simple solution , but maybe this will help.
const archivo = require("fs");
const json = require("fs");
const file = archivo.readFileSync('agenda.txt', 'utf8');
const lines = file.split('\n');
let campos;
let rawdata = json.readFileSync('agenda.json');
let json1 = JSON.parse(rawdata);
for (i in lines) {
if(lines[i]) {
campos = lines[i].split(",");
let payload = {
'Name' : campos[0].substr(1,campos[0].length-2), // to remove double quotes from the string
'Surname' : campos[0].substr(1,campos[0].length-2),
'Email' : campos[0].substr(1,campos[0].length-2),
'Phone' : campos[0].substr(1,campos[0].length-2),
}
json1.agenda.push(payload);
}
};
let data = JSON.stringify(json1);
json.writeFileSync('agenda2.json', data);
I have a string that could be in this form : 'school,room,teacher,pencil' or it could be 'school,room,teacher' .
What I want to do is after splitting the string , every word should have this structure :
{node:word,group:word-1,groupN:word-2 ...}
For example for the word room the structure would be {node:"room",group:"school"}
for the word teacher it would be {node:"teacher",group:"room",groupN:"school"}
for the word pencil it would be {word:"pencil",group:"teacher",groupN:"room",groupNN:"school"}
I did like that but it's not standard way :
var str="xxx,xxx,xxx,xxx"
var splitedStr = str.split(",")
for(var i=0;i<splitedStr.length;i++) {
var data = {node :splitedStr[i],group:splitedStr[i-1],groupN:splitedStr[i-2]}
}
Please do you have any idea how can i do that ?
Try this:
const getJson= (elName, textElements) => {
const elements = textElements.split(',');
const elementIdx = elements.findIndex(el => el===elName)
let result = {room:elName}
if (elementIdx>=0)
elements.slice(0,elementIdx).map((el, idx)=>{
result['group'+Array(elementIdx - idx).join("N") ] = el
})
return result
}
Ad use like this:
const arr = 'school,room,teacher,pencil'
console.log(getJson('room', arr))
console.log(getJson('teacher', arr))
console.log(getJson('pencil', arr))
I have the following object:
Object { WK23: 12.52573059875292, WK22: 122.2625975869411, WK21:
78.48714311048059, WK20: 87.14214810403018, WK26: 78.52625051674245, WK25: 77.64480983891451, WK24: 67.42158281711342, WK2:
78.420343898902, WK3: 77.91344340707354, WK4: 77.29048185059888 }
Is it possible to sort it by keys, from WK1 to WK100 ?
How to do this in JavaScript ?
Thanks in advance,
Try this , you can change order and assign to different object
var unordered={ WK23: 12.52573059875292, WK22: 122.2625975869411, WK21: 78.48714311048059, WK20: 87.14214810403018, WK26: 78.52625051674245, WK25: 77.64480983891451, WK24: 67.42158281711342, WK2: 78.420343898902, WK3: 77.91344340707354, WK4: 77.29048185059888 }
console.log(JSON.stringify(unordered));
const ordered = {};
Object.keys(unordered).sort(function(a,b){
var a=a.split("WK")[1];
var b=b.split("WK")[1];
return a-b
}).forEach(function(key) {
ordered[key] = unordered[key];
});
console.log(JSON.stringify(ordered));
No, you can't. Object keys are not ordered. If you want to sort entries in an Object, you will have to convert it into an Array.
var obj = {"WK23": 12.52573059875292, "WK22": 122.2625975869411};
var result = Object.keys(obj).map(function(key) {
return [key, obj[key]];
});
// the result Array can then be sorted...
// result = [["WK23": 12.52573059875292], ["WK22": 122.2625975869411]]
var sorted = result.sort(function(a,b){
var num_a = parseInt(a[0].replace("WK", ""));
var num_b = parseInt(b[0].replace("WK", ""));
return num_a > num_b;
});
// sorted = [["WK22": 122.2625975869411], ["WK23": 12.52573059875292]]
here you go
const sample = { WK23: 12.52573059875292, WK22: 122.2625975869411, WK21:
78.48714311048059, WK20: 87.14214810403018, WK26: 78.52625051674245, WK25: 77.64480983891451, WK24: 67.42158281711342, WK2:
78.420343898902, WK3: 77.91344340707354, WK4: 77.29048185059888 };
const sorted = Object.keys(sample).sort().map(key => sample[key]);
please check out this fiddle.
This has an outerHTML var. Now, I have to create it as a list of objects ; objects will be each row.
In this example the row number is 3 , but I will get it in a var like
var maxrow = $('#rownumber').val();
var TotalObject = [] ;
that will contain :
{
{column1header :row1col1value ,column2header :row1col2value ,column3header :row1col3value,column4header :row1col4value },
{column1header :row2col1value ,column2header :row2col2value ,column3header :row2col3value,column4header :row2col4value },
{column1header :row2col1value ,column2header :row2col2value ,column3header :row2col3value,column4header :row2col4value }
}
Then, I have to send this list of objects to my controller action method with an ajax call :
[httppost]
public PartialViewResult MyAction(list<mymodel> model)
{
return Partialview ("mypartialview",model);
}
mymodel also has IEnumerable<mymodel> where I have to transform this to that.
How about this approach :
Clean the input with a regular expression
Split it into an array
Create an iterator for your data
Push the result into a JSON object
Send the JSON result with Ajax?
EDIT: I improved the cleanup regular expressions ; it should account for empty values now.
It is based on the HTML markup, which isn't valid in your example (missing </tr> or <tr> tags).
var outerhtml = "\r\n\r\n<thead>\r\n\r\n<tr>\r\n\r\n<th>\r\n\r\ncolumn1header\r\n\r\n\r\n\r\n</th>\r\n\r\n<th>\r\n\r\ncolumn2header\r\n\r\n</th>\r\n\r\n<th>\r\n\r\ncolumn3header\r\n\r\n</th>\r\n\r\n<th>\r\n\r\ncolumn4header\r\n\r\n</th>\r\n\r\n</tr>\r\n\r\n</thead>\r\n\r\n<tbody>\r\n\r\n<tr>\r\n\r\n<td>\r\n\r\nrow1col1value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\nrow1col2value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\nrow1col3value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\nrow1col1value\r\n\r\n</td>\r\n\r\n</tr>\r\n\r\n<tr>\r\n\r\n<td>\r\n\r\nrow2col1value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\nrow2col2value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\nrow2col3value</td>\r\n\r\n<td>\r\n\r\nrow2col4value\r\n\r\n</td>\r\n\r\n</tr>\r\n\r\n<td>\r\n\r\nrow3col1value\r\n\r\n</td>\r\n\r\n<td>\r\n\r\n row3col2value\r\n\r\n </td>\r\n\r\n<td>\r\n\r\nrow3col3value\r\n\r\n</td>\r\n\r\n <td>\r\n\r\n row3col4value \r\n\r\n </td>\r\n\r\n</tbody>\r\n\r\n";
const data = outerhtml.split("\r\n").join("");
const [_,h,v] = /(?:<thead><tr>(.*)<\/tr><\/thead>)(?:<tbody>(.*)<\/tbody>)/igm.exec(data);
const headers = h.split("</th><th>")
.map(t => t.replace("<th>","")
.replace("</th>",""));
const values = v.match(/(?:<td>\s*(\s*\w*)\s*<\/td>)/igm)
.map(t => t.replace("<td>","")
.replace("</td>","")
.trim());
const iterator = function(array){
const coll = [...array];
return {
[Symbol.iterator](){
let nextIndex = 0;
return {
next: function(){
const _r = {
[headers[0]] : coll[nextIndex],
[headers[1]] : coll[nextIndex+1],
[headers[2]] : coll[nextIndex+2],
[headers[3]] : coll[nextIndex+3]
};
nextIndex += 4;
return nextIndex <= coll.length ?
{value: _r, done: false} :
{done: true};
}
}
}
}
}
let json=[];
const tuples = iterator(values);
for(tuple of tuples){
json.push(tuple);
}
console.log(json);
I want to insert an id in to an existing array of objects using underscorejs.
input array
var arr = [{name:"iPhone"}, {name:"Samsung"}];
expected output array
var arr = [{name:"iPhone", owner_id: "555"}, {name:"Samsung", owner_id: "555"}];
Tried following, its working but looking for a better way
devices = []
arr.forEach(function(data) {
var device = {};
device.owner_id = "555";
device.name= data.name;
devices.push(device )
})
zip them first and then using map
var arr = [{name:"iPhone"}, {name:"Samsung"}];;
var ageArr = [{owner_id: "555"},{owner_id: "555"}];
var newArr = _.zip(arr, ageArr).map(function(v) {
return _.extend(v[0], v[1]);
});
console.log(newArr);
<script src="http://underscorejs.org/underscore-min.js"></script>
No need to use underscore. Here is an easy way using the .map method. Each object returned inside the callback will be inserted into the devices array.
var devices = arr.map(function(data) {
var device = {};
device.owner_id = "555";
device.name= data.name;
return device;
})
If you console.log devices it will print (depending on data of course):
[{name:"iPhone", owner_id: "555"}, {name:"Samsung", owner_id: "555"}];