Return multiple lines instead of console.log - javascript

I have the following code that produces the output I need using console.log
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
for(i = 0; i < productId.length; i++) {
console.log(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);
}
}
product()
console.log result -
1234|543210|id1|product1|10
1234|543210|id2|product2|20
1234|543210|id3|product3|30
1234|543210|id4|product4|40
When changing console.log to return, only the first line is returned. return result -
1234|543210|id1|product1|10
Is there a way to return the same results as console.log?

You would usually push the lines to an array and return the array
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
var ret = [];
for(i = 0; i < productId.length; i++) {
ret.push(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);
}
return ret;
}
let data = product();
console.log(data); // array holding the lines
// or join it with a newline
console.log(data.join("\n")); // lines as string

Push the results into an array then use the Array#join function.
function product() {
const advertiserId = 1234;
const ord = 543210;
const productId = "id1|id2|id3|id4".split("|");
const productName = "product1|product2|product3|product4".split("|");
const productItemPrice = "10|20|30|40".split("|");
const results = [];
for (let i = 0; i < productId.length; i++) {
results.push(`${advertiserId}|${ord}|${productId[i]}|${productName[i]}|${productItemPrice[i]}`);
}
return results;
}
const data = product();
console.log(data.join('\n'));

You could also use other console commands:
console.dir(data);

When you return from a function, it won't continue to execute. In your case, you can store each result and return them all at once.
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
var results = [];
for(i = 0; i < productId.length; i++) {
results.push(advertiserId + "|" + ord + "|" + productId[i] + "|" + productName[i]+ "|" +productItemPrice[i]);
}
return results;
}
results is now an array of strings that you can do what you want with.

Write the output to a variable then return. using return inside the loop will exit the loop
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
var output = "";
for(i = 0; i < productId.length; i++) {
output += advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i] + "\n";
}
return output;
}
product()

I think what you need is to store every result of the iteration on a variable, and then after the for loop you return it. Like
var a;
for loop... {
a += (all of your stuff) + '\n'
}
return a;
This should keep the break lines into the variable and so keep the indentation you want like console.log but into a variable.
If you need to have separated values you could store every line into a array and return the array after the for loop.

A return statement stops the function immediately and can only return a single value. You can add up data in the loop and then return at the end.
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
var result = [];
for(i = 0; i < productId.length; i++) {
result.push(advertiserId+"|"+ord+"|"+productId[i]+"|"+productName[i]+"|"+productItemPrice[i]);
}
return result;
}
product()

Can map() the values into an array and return the array or array joined to string
function product() {
var advertiserId = 1234;
var ord = 543210;
var productId = "id1|id2|id3|id4".split("|");
var productName = "product1|product2|product3|product4".split("|");
var productItemPrice = "10|20|30|40".split("|");
return productId.map(function(id, idx) {
return [
advertiserId,
ord,
productId[idx],
productName[idx],
productItemPrice[idx]
].join('|')
}).join('\n')
}
console.log(product());

Related

How do I merge JSON objects into 1 JSON object

Given:
var result1 = [{'p1':'v1'}];
var result2 = [{'p2':'v2'}];
var array1 = [{'p3':'v3'},{'p4':'v4'}];
Rules:
If array has one property, add property to main array to return.
If array has multiple properties, add a label and keep array as is.
https://jsfiddle.net/3unx0hsa/5/
function mergeJson(data) {
let newarray1 = [];
for (let index = 0; index < resultsArray.length; index++) {
let element = resultsArray[index][0];
if (element.length === 1) {
newarray1.push(element);
}
if (element.length > 1) {
var x = `{data${index}: ${element}`;
newarray1.push(x);
}
}
}
Illustration:
Template string literal creates a string. You need an object literal instead
var x = {[`data${index}`]: element};
var result1 = [{'p1': 'v1'}];
var result2 = [{'p2': 'v2'}];
var array1 = [{'p3': 'v3'}, {'p4': 'v4'}];
let x = mergeJson([result1, result2, array1]);
console.log(x);
function mergeJson(resultsArray) {
let newarray1 = [];
for (let index = 0; index < resultsArray.length; index++) {
let element = resultsArray[index];
if (element.length === 1) {
newarray1.push(element[0]);
}
if (element.length > 1) {
var x = {[`data${index}`]: element};
newarray1.push(x);
}
}
return newarray1;
}
After this line: var x = `{data${index}: ${element}`;, the value for x is a string. That is what you are seeing in you output. Change that line to something like this:
var x = {`{data${index}`: element};
That should give you the result you're expecting.
Using that type of function you will be able to display Json values
//JSON = objects and you have to call them for examle:
var result1 = [{'p1':'v1'}];
var result2 = [{'p2':'v2'}];
var array1 = result1.concat(result2);
for (i in array1){
array1[i];
for(x in array1[i]){
document.getElementById("test").innerHTML += x+" - "+array1[i][x] +" <br>";
}
}
<div id="test"></div>

Remove duplicates from string using jquery?

How i Remove duplicates from my string
var string="1,2,3,2,4,5,4,5,6,7,6";
But i want like this
var string="1,2,3,4,5,6,7";
Yes you can do it easily, Here is the working example
data = "1,2,3,2,4,5,4,5,6,7,6";
arr = $.unique(data.split(','));
data = arr.join(",");
console.log(data);
Create the following prototype and use it to remove duplicates from any array.
Array.prototype.unique = function () {
var arrVal = this;
var uniqueArr = [];
for (var i = arrVal.length; i--; ) {
var val = arrVal[i];
if ($.inArray(val, uniqueArr) === -1) {
uniqueArr.unshift(val);
}
}
return uniqueArr;
}
Ex:
var str = "1,6,7,7,8,9";
var array1 = str.split(',');
var array1 = array1.unique();
console.log(array1); // [1,6,7,8,9]
str = array1.join();
Use the following to push unique values into a new array.
var names = [1,2,2,3,4,5,6];
var newNames = [];
$.each(names, function(index, value) {
if($.inArray(value, newNames) === -1)
newNames.push(value);
});

Generic code for Arrays

I want to create one loop that will access and push data from three arrays in JavaScript:
var tempArray1=new Array();
var tempArray2=new Array();
var tempArray3=new Array();
I tried following code:
for(var j=1; j<4; j++) {
var res = new Array();
var str = 'tempArray' + j;
res = str.split(" ");
}
but with this nothing happened.
Please help me to create generic code.
Brief
var res=[...tempArray1,...tempArray2,...tempArray3]
or more dynamic
var res=eval('[...tempArray1,...tempArray2,...tempArray3]') // since it is generic
DEMO :
var tempArray1=["I","love","JS"],tempArray2=["But","I'm"],tempArray3=["a crazy","JS","Programmer"]
function range(size){ /* for you case ,it returns : [1,2,3] */
return Array.from({length:size},(v,k)=>k+1)
}
function generate(size){
return eval('[...'+range(size).map((i)=>'tempArray'+i).join(',...')+']')
}
console.log(
generate(3)
)
var tempArray1 =[1,2,3];
var tempArray2 =[4,5];
var tempArray3 =[6];
function myConcat(){//This is the generic method
var result =[];
for (var i = 0; i < arguments.length; i++) {
result = result.concat(arguments[i]);
}
return result;
};
var conctenatedArray = myConcat(tempArray1,tempArray2,tempArray3);
console.log(conctenatedArray);
https://jsfiddle.net/8jwyzn0x/
**
OR
**
It would be good, if you wrap those array in a container object.
function myConcat(container){//This is the generic method
var result =[];
for(var i in container){
result = result.concat(container[i]);
}
return result;
};
var arrayContainer ={};
arrayContainer.tempArray1 =[1,2,3];
arrayContainer.tempArray2 =[4,5];
arrayContainer.tempArray3 =[6];
var conctenatedArray = myConcat(arrayContainer);
console.log(conctenatedArray);
https://jsfiddle.net/8jwyzn0x/1/

Push different object in an array with a for loop

I have an element structured like this:
Element ->
[{values: arrayOfObject, key:'name1'}, ... ,{values: arrayOfObjectN, key:'nameN'}]
arrayDiObject -> [Object1, Object2, ... , ObjectN] //N = number of lines in my CSV
Object1 -> {x,y}
I have to take data from a big string:
cityX#substanceX#cityY#substanceY#
I thought to make it this way, but it seems like it pushes always in the same array of objects. If I put oggetto = {values: arrayDateValue, key: key}; inside the d3.csv function, instead if I put outside the function it add me only empty objects.
Here is my code:
var final = new Array();
var oggetto;
var key;
function creaDati() {
var newdate;
var arrayDateValue = new Array();
var selString = aggiungiElemento().split("#");
//selString is an array with selString[0]: city, selString[1]: substance and so on..
var citySelected = "";
var substanceSelected = "";
for (var i = 0; i < selString.length - 1; i++) {
if (i % 2 === 0) {
citySelected = selString[i];
} else if (i % 2 !== 0) {
substanceSelected = selString[i];
key = citySelected + "#" + substanceSelected;
d3.csv("/CSV/" + citySelected + ".csv", function(error, dataset) {
dataset.forEach(function(d) {
arrayDateValue.push({
x: d.newdate,
y: d[substanceSelected]
});
});
});
oggetto = {
values: arrayDateValue,
key: key
};
arrayDateValue = [];
final.push(oggetto);
}
}
}
Any idea ?
First you should make the if statement for the city and then for the key, which you seem to be doing wrong since you want the pair indexes to be the keys and the not pair to be the city, and you are doing the opposite. And then you need to have the d3.csv and push the objects outside of the if statement, otherwise in your case you are just adding elements with citySelected="".
Try something like :
for(var i = 0; i < selString.length -1; i+=2){
cittySelected = selString[i];
substanceSelected = selString[i+1];
key = citySelected + "#" + substanceSelected;
d3.csv("/CSV/"+citySelected+".csv", function(error, dataset){
dataset.forEach(function(d){
arrayDateValue.push({x: d.newdate, y: d[substanceSelected]});
});
});
oggetto = {values: arrayDateValue, key: key};
arrayDateValue = [];
final.push(oggetto);
}
It's is not the best way to do it, but it is clearer that what you are following, i think.
In the if(i % 2 == 0) { citySelected = ... } and else if(i % 2 !== 0) { substanceSelected = ... } citySelected and substanceSelected will never come together.
The values should be in one statement:
if(...) { citySelected = ...; substanceSelected = ...; }
The string can be splitted into pairs
city1#substance1, city2#substance2, ...
with a regex (\w{1,}#\w{1,}#).
Empty the arrayDateValue after the if-statement.
Hint:
var str = "cityX#substanceX#cityY#substanceY#";
function createArr(str) {
var obj = {};
var result = [];
var key = "";
// '', cityX#substanceX, '', cityYsubstanceY
var pairs = str.split(/(\w{1,}#\w{1,}#)/g);
for (var i = 0; i < pairs.length; i++) {
if(i % 2 !== 0) {
key = pairs[i];
// d3 stuff to create values
obj = {
// Values created with d3 placeholder
values: [{x: "x", y: "y"}],
// Pair
key: key
};
result.push(obj);
}
// Here should be values = [];
}
return result;
}
var r = createArr(str);
console.log(r);
May be you can do like this;
var str = "cityX#substanceX#cityY#substanceY",
arr = str.split("#").reduce((p,c,i,a) => i%2 === 0 ? p.concat({city:c, key:a[i+1]}) : p,[]);
console.log(JSON.stringify(arr));
RESOLVED-
The problem is about d3.csv which is a asynchronous function, it add in the array when it finish to run all the other code.
I make an XMLHttpRequest for each csv file and it works.
Hope it helps.

How to count the JSON object and on the basis of count take the same output

How to count the JSON object and on the basis of count take the same output
var obj =
[
{"id":"0","name":"Mike Johnson","group":1},
{"id":"1","name":"Bob Smith","group":2},
{"id":"2","name":"Richard Thomas","group":3},
{"id":"3","name":"Betty White","group":16},
{"id":"4","name":"Tim Thompson","group":3},
{"id":"5","name":"Carl Lewis","group":16},
{"id":"6","name":"Kathy Towers","group":3},
{"id":"7","name":"Billy Bob","group":1},
{"id":"8","name":"Sally Bailey","group":1}
];
First I would like the count after it on the basis of count. I want same output like input.
for Count:-
var count = 0;
function getCount() {
for (var i = 0; i < obj.length; i++) {
count++;
}
return count;
}
for output :-
function showDetails() this is not giving the proper output
{
for(var j=0; j< count; j++){
obj.push([{j}]);
}
alert(obj.name);
}
alert(showDetails());
And I want an output like:-
var obj =
[
{"id":"0","name":"Mike Johnson","group":1},
{"id":"1","name":"Bob Smith","group":2},
{"id":"2","name":"Richard Thomas","group":3},
{"id":"3","name":"Betty White","group":16},
{"id":"4","name":"Tim Thompson","group":3},
{"id":"5","name":"Carl Lewis","group":16},
{"id":"6","name":"Kathy Towers","group":3},
{"id":"7","name":"Billy Bob","group":1},
{"id":"8","name":"Sally Bailey","group":1}
];
Can anybody help me please?
var data ="January,February,March,April,May,June,July,August,September,October";
var obj = data.split(',').map((item)=>{
return {
name:item
}
});
obj will be the desired output
var str = "January,February,March,April,May,June,July,August,September,October";
var arr = str.split(',').map(function(v) {
return {name: v};
});
console.log(arr);
var str = "January,February,March,April,May,June,July,August,September,October";
var months = str.split(",");
var result = [];
for (i in months)
{
var month = {};
month.name = months[i];
//you can do more things else here, for example:
//month.monthOfYear = (i+1);
//month.numberOfDay = 123123123;
result.push(month);
}
You can do something like this:
var array = string.split(",");
var finalArray = [];
array.forEach(function(item){
var obj = {
name: item
}
finalArray.push(obj);
});
console.log(finalArray);
MDN reference
use var array = string.split(',');
For a more ES2015 heavy version. Constants, arrow function and implicit return statement.
const str = 'January,February,March,April,May,June,July,August,September,October'
const result = str.split(',').map(name => ({name}))
console.log(result)

Categories

Resources