-1

In javascript I am in situation where i need to make variable arguments based on a length of an array, below is an sample code

function getValesList(json){
    return getValues(json[0])+getValues(json[1])+getValues(json[2]);
}

function getValues(json1){
    let valueList = Object.values(json1);
    let valueListPipe = valueList.join("|");
    return valueListPipe+lineSeparator;
}

where json is an array of JSON objects and I need to make a pipe delimiter file based on the length of incoming array. How to make it dynamic where I can do like a varargs in JAVA

1 Answers1

0

If you're just passing N arguments of the same type, you can use the rest feature of Javascript for function arguments.

function getValuesList(...json){
    return json.map(j => getValues(j)).join("");
}

This allows you go pass any number of separate arguments as in getValuesList(o1, o2, o3, o4) and the json parameter within your function will automatically be an array of however many arguments were passed.

jfriend00
  • 683,504
  • 96
  • 985
  • 979