Related
Trying below code but not getting expected output
I am expecting below output
[{"label":"one","value":"1","disabled":true},{"label":"two","value":"2","disabled":true},{"label":"three","value":"3","disabled":false},{"label":"four","value":"4","disabled":false},{"label":"five","value":"5","disabled":false},{"label":"six","value":"6","disabled":true}]
// ---
var A1 = [ {label:"one", value:"1"}, {label:"two", value:"2"}, {label:"six", value:"6"}];
var A2 = [ {label:"one", value:"1","disabled":false}, {label:"two", value:"2","disabled":false}, {label:"three", value:"3","disabled":false}, {label:"four", value:"4","disabled":false}, {label:"five", value:"5","disabled":false}, {label:"six", value:"6","disabled":false}];
for(let i = 0 ; i <A2.length; i++){
for(let j = 0; j < A1.length; j++){
if(A1[j].value == A2[i].value){
A2[i].disabled = true;
}
else{
A2[i].disabled = false;
}
}
}
console.log( JSON.stringify( A2 ) );
If I understand correctly what you're trying to do, the following should do it:
for(let i = 0 ; i <A2.length; i++){
A2[i].disabled = false;
for(let j = 0; j < A1.length; j++){
if(A1[j].value == A2[i].value){
A2[i].disabled = true;
break;
}
}
}
That is - start off with the false value (no match), and only set it to true if you find a match. (Then break out of the inner loop, because there's no need to continue.)
Here is an alternative perspective (jQuery option) if you want it. This is just an idea as I am sure this can be written more elegantly :)
var A1 = [ {label:"one", value:"1"}, {label:"two", value:"2"}, {label:"six", value:"6"}];
var A2 = [ {label:"one", value:"1","disabled":false}, {label:"two", value:"2","disabled":false}, {label:"three", value:"3","disabled":false}, {label:"four", value:"4","disabled":false}, {label:"five", value:"5","disabled":false}, {label:"six", value:"6","disabled":false}];
var tempA = new Array();
var resultArray = new Array();
$.each(A1,function(datakey,dataval){
tempA.push(dataval['label']);
});
$.each(A2,function(datakey,dataval){
if ($.inArray(dataval['label'],tempA)>-1) {
resultArray.push({'label':dataval['label'],'value':dataval['value'],'disabled':true})}
else {
resultArray.push({'label':dataval['label'],'value':dataval['value'],'disabled':false})
}
});
console.log(resultArray);
I have data like this.
var abc =",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
abc = abc.split(",");
let stub={};
var results=[];
var key=["name","value","acc"];
var i=0;
var j=0;
for( var i = 0 ; i <abc.length - 1;i++){
stub[key[j]=abc[i];
j++
if(j==3){
results.push(stub);
stub={};
j=0;
}
}
abc = results;
I would like to get those values arranges in form of array of object having those 3 keys:
output should be:
abc = [{"name": "paul", "value": "2000","acc":"12sc21"},{"name":"logan","value":"123","acc":"21sdf34"},{"name":"vfsarwe","value":"456456","acc":"32fd23"}];
but not able to get the desired output. this output only comes when string don't have ,,,,,, in starting. But the data i'm getting is sometimes having ,,,,, in stating.
You can use abc.replace(/(^[,\s]+)/g, '') to remove leading commas or whitespace from the String. Your for loop is also not running for long enough; it is looping until there is only one element left in the Array and then stopping.
Change
for(var i = 0 ; i < abc.length-1; i++)
To
for(var i = 0 ; i < abc.length; i++)
var abc =",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
abc = abc.replace(/(^[,\s]+)|([,\s]+$)/g, '').split(",");
let stub={};
var results=[];
var key=["name","value","acc"];
var i=0;
var j=0;
for(var i = 0 ; i < abc.length; i++){
stub[key[j]]=abc[i];
j++
if(j==3){
results.push(stub);
stub={};
j=0;
}
}
abc = results;
console.log(abc);
You can use .replace(/^\,+/, '') to remove all leading commas, then split by comma to get an array, then loop over this array using 3 as step and construct your results:
var abc = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var arr = abc.replace(/^\,+/, '').split(",");
var results = [];
for (var i = 0; i < arr.length; i = i + 3) {
results.push({
"name": arr[i],
"value": arr[i + 1],
"acc": arr[i + 2]
});
}
Demo:
var abc = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var arr = abc.replace(/^\,+/, '').split(",");
var results = [];
for (var i = 0; i < arr.length; i = i + 3) {
results.push({
"name": arr[i],
"value": arr[i + 1],
"acc": arr[i + 2]
});
}
console.log(results);
You are on the right track with splitting your data on ,. You can then split the data in to chunks of 3, and from there map each chunk to a dict.
var data = ",,,,,,,,,,,,,,,paul,2000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var split = data.split(",");
var chunked = [];
while (split.length) {
chunked.push(split.splice(0,3));
}
var res = chunked.map((i) => {
if (!i[0] || !i[1] || !i[2]) {
return null;
}
return {
name: i[0],
value: i[1],
acc: i[2]
};
}).filter((i) => i !== null);
console.log(res);
You can use:
abc.replace(/,+/g, ',').replace(/^,|,$/g, '').split(',');
The regEx replaces removes the data that you are not interested in before performing the split.
or
abc.split(',').filter(Boolean);
The filter(Boolean) will remove the items from the array that could be the equivalent of false once the array has been instantiated.
EDIT:
var abc =",,,,,,,,,,,,,,,paul,2,000,12sc21,logan,123,21sdf34,vfsarwe,456456,32fd23";
var array = abc.replace(/,+/g, ',').replace(/^,|,$/g, '').split(/,([0-9,]+),/);
array = array.filter(Boolean).reduce(function(acc, item) {
if (item.match(/^[0-9,]+$/)) {
acc.push(item);
} else {
acc = acc.concat(item.split(','));
}
return acc;
}, []);
So I have this function where I've need to take out the evens and odds and put them into separate arrays but I need the evens array to print first rather than the odds.
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider(numbersArray) {
var evensOdds = [[], []];
for (var i = 0; i < numbersArray.length; i++) {
evensOdds[i & 1].push(numbersArray[i]);
}
return evensOdds;
}
If you want to split the number by their even and odd values, instead of using the index (i), determine the sub array to push into using the value - numbersArray[i] % 2.
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider(numbersArray) {
var evensOdds = [[], []];
for (var i = 0; i < numbersArray.length; i++) {
evensOdds[numbersArray[i] % 2].push(numbersArray[i]);
}
return evensOdds;
}
console.log(divider(numbersArray));
If you want to split them by even and odd indexes use (i + 1) % 2 to determine the sub array to push into:
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
function divider(numbersArray) {
var evensOdds = [[], []];
for (var i = 0; i < numbersArray.length; i++) {
evensOdds[(i + 1) % 2].push(numbersArray[i]);
}
return evensOdds;
}
console.log(divider(numbersArray));
Just for fun, a forEach version of the accepted answer.
var numbersArray = [1,2,34,54,55,34,32,11,19,17,54,66,13];
var even_odd = [ [], [] ];
numbersArray.forEach( e => even_odd[e%2].push(e) );
console.log(even_odd);
I have an object with duplicate values and I want to count all those which have the same value and remove them.
var myArray = [{nr: 'bbc',}, {nr: 'bbc'}, {nr: 'bbc'}, {nr: ccc}];
from this array I want to create another array but remove the duplicated values and count them to be like this.
var myArray = [{nr: 'bbc',amount: 3}}, {nr: ccc,amount: 1}];
You could probably use a better format
var count = {};
for(var i = 0; i < myArray.length; ++i) {
if(typeof count[myArray[i].nr] == 'undefined') {
count[myArray[i].nr] = 0;
}
++count[myArray[i].nr];
}
and this wound yield somehing like:
count = {
bcc: 3,
ccc: 1
};
if you still need it with the structure you specified, then:
var newArray = [];
for(var k in count) {
newArray.push({
nr: k,
amount: count[k]
});
}
If you want the same structure, this will work for you
var newArray = [];
for (var i = 0; i < myArray.length; i++) {
var matched = false;
for (var j = 0; j < newArray.length; j++) {
if(myArray[i].nr === newArray[j].nr){
matched = true;
newArray[j].amount++;
break;
}
};
if(!matched)
newArray.push({nr:myArray[i].nr,amount:1});
};
console.log(newArray);
so what i have is three (or more) array with different sizes something like the following :
a ['x1' , 'x2', 'x3'];
b ['y1','y2']
c ['z1']
i want to create a string like the following :
x1,y1,z1 - x2,y2 - x3
any idea how the logic behind doing something like this?
Just because I like to be contrary (who'd a thunk it!), here's an alternative using plain for loops:
var data = [
['q1','q2'],
['x1', 'x2', 'x3'],
['y1', 'y2', 'y3', 'y4', 'y5'],
['z1']
];
var result = [];
for (var i=0, iLen=data.length; i<iLen; i++) {
temp = data[i];
for (var j=0, jLen=temp.length; j<jLen; j++) {
if (!result[j]) {
result.push([temp[j]]);
} else {
result[j].push(temp[j]);
}
}
}
alert(result.join(' - ')); // q1,x1,y1,z1 - q2,x2,y2 - x3,y3 - y4 - y5
If support for sparse arrays is required (i.e. with missing members or values of undefined or null or whatever), that's not too hard to implement.
var arrays = [a, b, c];
var groupings = [];
var idx = 0;
while (true) {
var items = [];
for (var i = 0; i < arrays.length; i++) {
if (arrays[i].length > idx) {
items.push(arrays[i][idx]);
}
}
if (!items.length)
break;
else
groupings.push(items.join(','));
idx++;
}
console.log(groupings.join(' - '));
Let's say you have an array of arrays of arbitrary number, then you can use code like this to iterate through them:
var data = [
['q1', 'q2'],
['x1', 'x2', 'x3'],
['y1', 'y2', 'y3', 'y4', 'y5'],
['z1']
];
var output = "";
var term, j = 0;
while (true) {
term = [];
for (var i = 0; i < data.length; i++) {
if (data[i].length > j) {
term.push(data[i][j]);
}
}
// if no more terms, then all arrays are exhausted
// so it's time to break out of the loop
if (term.length == 0) {
break;
}
if (output) {
output += " - ";
}
output += term.join(",");
j++;
}
alert(output);
And, a working example here: http://jsfiddle.net/jfriend00/fZKbp/