My loop function is as below
var resources= jsonObj.entry;
var resourceType;
var i;
for (i = 0; i < resources.length; i++) {
resourceType += resources[i];
}
console.log(resourceType)
if i do jsonObj.entry[0] i get the first entry so i implemented for-loop to get all the entries but console.log on resourceType prints the following
So, one way to do it is
var resources = jsonObj.entry;
var resourceTypeArray = [];
var resourceType = "";
for (let item = 0; item <= resources.length; item++) {
// This will remove all cases where the item doesn't exist and/or resource doesn't exist
if(resources[item] && resources[item].resource){
resourceTypeArray.push(resources[item].resource);
resourceType += JSON.stringify(resources[item].resource);
}
}
// printing out to the console the array with resources
console.info(resourceTypeArray);
// printing out to the console the string with the concatenation of the resources
console.info(resourceType);
I also created a StackBlitz with the working solution and your json file kindly provided.
hope it helps.
I have a result of type of:
EMailLabel: "Mailing address"
LogLabel: "User login"
LoginButton: "Enter the program"
And in order to manipulate this result by splinting it into pairs, I need to convert it into string using the following:
function parse(str, separator) {
var parsed = {};
var pairs =
str.toString().split(separator);
for (var i = 0, len = pairs.length, keyVal; i < len; ++i) {
keyVal = pairs[i].split("=");
if (keyVal[0]) {
parsed[keyVal[0]] = keyVal[1];
}
}
return parsed;
}
But in the instruction:
str.toString().split(separator);
returns me the value of:
{[object Object]: undefined}
And of nothing is turned into string.
If I use the same instruction like that:
str.split(separator);
Threw me the error of:
Uncaught TypeError: str.split is not a function
And from what I've searched on the web I saw that I have to convert the str which is a Hashtable result into string.
I did that but unfortunately without any success
Is someone to help me on this issue?
Looks like you need something like this:
function parse(map, separator) {
return Object.keys(map).reduce((data, key) => {
data.push(`${key}${separator} "${map[key]}"`);
return data;
}, []).join('\n');
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Error Message:
TypeError: Cannot read property "length" from undefined.
Sample Data:
Data
The error message appear after highlighted the sample data and clicked the "Button" that has an Assign script on it. Script is:
function result(range) {
var output2 = [];
for(var i=0, iLen=range.length; i<iLen; i++) {
var s = range[i][1].split(" ");
for(var j=0, jLen=s.length; j<jLen; j++) {
var output1 = [];
for(var k=0, kLen=range[0].length; k<kLen; k++) {
if(k == 1) {
output1.push(s[j]);
} else {
output1.push(range[i][k]);
}
}
output2.push(output1);
}
}
return output2;
}
(saw this code online and trying to make it work, but failed to)
Simply put the error indicates that you have an undefined array.
Looking at your code there are 2 places this could happen. Please check that the following are true:
range is an array passed on to the function (the function cannot be called on its own, somewhere else you must have result(arrayVar) calling this function)
range variable is an array that contains arrays, so it must be at the very least [[]] (for example var range = [[a1,a2,a3],[b1,b2,b3]]) would produce range.lenght = 2 and range[0].lenght = 3, however if you have [a1, a2, b1, b2], then range.lenght = 4 and range[0].lenght will not work
I am writing a cleansing function which will delete all invalid docs from collection. For this I have an idea to push invalid _id values into array variable and delete with $in.
function(scan){
var err
for(var n=1;n<scan;n++){
var doc = db.zeroDimFacts.findOne({_id:n}) ,nKeys =0;
for(k in doc)
nKeys++
if(nKeys <5)
err = n.toArray()
}
After I push all values to err Array, I have a script to delete matching docs. However, there is something missing with the code which throws me error at n.toArray() .
Can someone help me to correct the code?
function(scan) {
var doc;
var nKeys;
var err = [];
for(var n = 1; n < scan; ++ n) {
doc = db.zeroDimFacts.findOne({_id: n})
nKeys = 0;
for(k in doc) {
++ nKeys;
}
if(nKeys < 5) {
err.push(n);
}
}
return err;
};
Pay attention to findOne() call. When it returns null, n will get into the array which seems undesirable to me.
You can use findByIdAndRemove({criteria}) and depending on return value you can have the logic.
I implemented a Java code for permutation combination a string with input str= "word1 word2 word3" then output as:
arr[0]= "word1 word2"
arr[1]= "word1 word3"
arr[2]= "word1 word2 word3"
arr[3]= "word2 word1"
arr[4]= "word2 word1 word3"
arr[5]= "word2 word3 word1"
arr[6]= "word3 word1"
arr[7]= "word3 word1 word2"
arr[8]= "word3 word2 word1"
This is Java code:
private static void permute(String[] ss, boolean[] used, String res, int level, List<String> list) {
if (level == ss.length && res != ""){
list.add(res);
return;
}
for (int i = 0; i < ss.length; i++) {
// Check if the string is currently used
if (used[i]) {
continue;
}
// Check if res is empty or a single word
if(level > 1)
list.add(res);
used[i] = true;
permute(ss, used, res + " " + ss[i], level + 1, list);
used[i] = false;
}
}
public static List<String> PermuteWords(String s){
String[] ss = s.split(" ");
boolean[] used = new boolean[ss.length];
String res = "";
List<String> list = new ArrayList<String>();
permute(ss, used, res, 0, list);
return list;
}
When converting to JS code, I don't know what are errors in this code:
function permute(ss, used, res, level, list){
if(level==ss.lenght&&res!==""){
list.add(res);
return;
}
for(var i=0; i<ss.lenght; i++){
if (used[i]===true){
continue;
}
if(level>1){
list.add(res);
used[i]=true;
permute(ss, used, res+" "+ss[i], level+1, list)
used[i]=false;
}
}
}
function permuteword(s){
var ss;
for(var i=0; i<s.length;i++){
ss[i]=s[i];
}
var used;
for(var j=0; j<s.length;j++){
used[j]=false;
}
var result;
permute(ss, used, res, 0, result);
return result;
}
Java and Javascript may sound almost the same but they aren't. They take different appoaches doing different things. If in Java You are checking the console for errors on running the program, then in Javascript you can check for the errors in the browser console (open console with F12)
POPULATE ARRAY
Javascript arrays don't have add() method. To add a value to it, use push:
list.push(res)
POPULATE ARRAY #2
In permuteword() function you are trying to populate variable ss that is not initialized. Compiler doesn't understand where you want to put that value. Initialize ss as an empty array:
var ss = [];
TYPOs
In the first for loop you have ss.lenght. Fix that. Always check for the typos.
EXTRA
In permuteword() you are passing res to the permute() function although you don't have it defined in the function. Things will work if res is a global variable defined outside of the functions.
And take advantage of that browser console. Every Javascript developer (from noobie to pro) has it always open!
I ran the code in couchdb and cloudant that showed the general errors that i missed checking the syntax. I modified the code as:
function permute(ss, used, res, level, list){
if(level==ss.lenght&&res!==""){
list.push(res);
return;
}
for(var i=0; i<ss.length; i++){
if (used[i]===true){
continue;
}
if(level>1){
list.push(res);
used[i]=true;
permute(ss, used, res+" "+ss[i], level+1, list)
used[i]=false;
}
}
}
function permuteword(s){
var ss=[];
for(var i=0; i<s.length;i++){
ss[i]=s[i];
}
var used=[];
for(var j=0; j<s.length;j++){
used[j]=false;
}
res="";
var result=[];
permute(ss, used, res, 0, result);
return result;
}
function(doc){
var list=permuteword("word1 word2 word3");
for(var i=0; i<list.length; i++){
index("default", list[i],{store, true});
}
}
However, i still meet the errors as:
{"error":"{nocatch,{compilation_error,<<\"Expression does not eval to a function. ((new String(\\"function permute(ss, used, res, level, list){\\r\\n if(level==ss.lenght&&res!==\\\\"\\\\"){\\r\\n list.push(res);\\r\\n return;\\r\\n }\\r\\n\\r\\n for(var i=0; i1){\\r\\n list.push(res);\\r\\n used[i]=true;\\r\\n permute(ss, used, res+\\\\" \\\\"+ss[i], level+1, list)\\r\\n used[i]=false;\\r\\n }\\r\\n }\\r\\n}\\r\\nfunction permuteword(s){\\r\\n var ss=[];\\r\\n ss=s.split(\\\\" \\\\");\\r\\n var used=[];\\r\\n for(var j=0; j>}}","reason":"[{couch_os_process,prompt,3,[{file,\"src/couch_os_process.erl\"},{line,65}]},\n {dreyfus_index_updater,update,2,\n [{file,\"src/dreyfus_index_updater.erl\"},{line,42}]}]"}