Take String From Javascript Array [duplicate] - javascript

This question already has answers here:
using .join method to convert array to string without commas [duplicate]
(4 answers)
Convert array to string javascript by removing commas [duplicate]
(3 answers)
Closed 1 year ago.
I have an input field with one button. I store value of input in array.
My console.log output has below result
How can take the String "JOHN" from this array?
My code is:
var category = $('#customer_name_search').val();
var len = category.length;
var arr = [];
for(var i=0; i<len; i++){
var test = category[i];
arr.push(test);
}
console.log(arr);

You might want to use the join() method
var category = $('#customer_name_search').val();
var len = category.length;
var arr = [];
for(var i=0; i<len; i++){
var test = category[i];
arr.push(test);
}
console.log(arr.join(''));
here is some documentation

Use Array.Join().
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
// expected output: "Fire,Air,Water"
console.log(elements.join(''));
// expected output: "FireAirWater"
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

you can use array.join to merge the strings in an array
const array = ["J", "O", "H", "N"];
const merged = array.join("");
so the result will be "JOHN"

You just need to join the array by using array function(join).
Check out this link
var category = $('#customer_name_search').val();
var str = category.join('');

Related

Create array with many arrays inside which are created from one big array on special condition [duplicate]

This question already has answers here:
Transposing a 2D-array in JavaScript
(25 answers)
Closed 2 years ago.
// n number of those
let array1 = [1,3,3,6]
let array2 = [4,7,3,8]
let array3 = [1,4,6,4]
// wanted
let final = [
[1,4,1], <-- first array in the final
[3,7,4],
[3,3,6],
[6,8,4]
]
First from each array (array1, array2, array3...) create first array in final one.
Second from each array create second one.. etc.
Any ideas?
You can do something like this:
const final = [];
for (let i = 0; i < array1.length; ++i) {
final[i] = [array1[i], array2[i], array3[i]];
}
console.dir(final);
you can try this
let array1 = [1,3,3,6]
let array2 = [4,7,3,8]
let array3 = [1,4,6,4]
finarray=[]
array1.forEach((x,i)=>{ finarray.push([x,array2[i],array3[i]])})
console.log(finarray)

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

Javascript: Combining Strings into Array [duplicate]

This question already has answers here:
Javascript: How to Combine & Filter Arrays
(4 answers)
Closed 5 years ago.
I have 3 strings that I need to convert into a single array, from there I want to filter out the type: "bundle".
I need to note that I'm using Javascript Code by Zapier and their javascript library is a bit limited as far as the functions that I can use, but this is what I have so far which works if I hard code itemArray. I'm just having trouble creating my itemArray from the 3 given strings:
Strings:
var type = 'bundle, simple, simple';
var name = 'Product1, Product2, Product3';
var price = '1.99, 2.99, 3.99';
I need to figure out how to convert the above 3 strings above into the following array using javascript:
var itemArray = [
{type:"bundle", info: {name: "Product1", price: "1.99"}},
{type:"simple", info: {name: "Product2", price: "2.99"}},
{type:"simple", info: {name: "Product3", price: "3.99"}}];
From there I'm looking to filter out the bundle product type and only return the simple product types, I'm doing that with the following code:
// Using a for loop
var filtered = [];
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item.type == 'simple') filtered.push(item);
}
return {filtered}; //this returns just the 2 simple product type arrays
So my question is, how do I take those 3 strings that I began with and convert those into my itemArray format using javascript?
First make the strings into arrays of the three strings you want. Then in a for loop you can push all of them in whatever (identical) format you want, since all 3 lists have 3 elements each. then you can use the filter function to easily filter out the bundle elements as below.
The following snippet will print out the item array and the filtered value you requested
var types = 'bundle, simple, simple'.split(", ");
var names = 'Product1, Product2, Product3'.split(", ");
var prices = '1.99, 2.99, 3.99'.split(", ");
var itemArray = [];
for(var i = 0; i < 3; i++){
itemArray.push({"type": types[i], "info":{"name": names[i], "price": prices[i]}});
}
console.log(itemArray);
var filtered = [];
for (var i = 0; i < itemArray.length; ++i) {
var item = itemArray[i];
if (item["type"] === 'simple') filtered.push(item);
}
console.log({filtered});
var type = 'bundle, simple, simple'.split(', '), // split the
nameArr = 'Product1, Product2, Product3'.split(', '), // strings to
priceArr = '1.99, 2.99, 3.99'.split(', '), // get the arrays
res = type.map((v,i) => Object.assign({}, {type: v, info: {name: nameArr[i], price: priceArr[i]}})), //map the objects with specified keys and values from given arrays
result = res.filter(v => v.type != 'bundle'); //remove the `bundle` type elements
console.log(result);

how to create array object using integer variable javascript [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]
You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

make array multidimensional values and key pairs in javascript [duplicate]

This question already has answers here:
Combining two arrays to form a javascript object
(11 answers)
Closed 7 years ago.
How i get below output in javascript
var keys = ["1","2","3"];
var values = ["one", "two","three"];
var final = {
"1": "one",
"2": "two",
"3": "three"
}
Loop over one of the arrays and use the index to access the other, save in an object
var result = {};
keys.forEach(function(item, index) {
result[item] = values[index];
});
A very simple loop should suffice, copying the values & keys at each index into a new object:
var finalOutput = {};
for(var i=0, j=keys.length; i<j; i++) {
finalOutput[keys[i]] = values[i];
}
Working Example
Note: You shouldn't use final as a variable name as it is a reserved future keyword

Categories

Resources