0

I have a list of hrefs with product data:name and it's id. First of all i removed prod Id that i'll use as separate variable. After this, the remained part of href should be used as the product name.

var prodHref = document.querySelectorAll('.widget-products-list-item-label');
  var allHref = [];
   for(var i=0;i<prodHref.length;i++){
  allHref.push(prodHref[i].href.split('/')[5])
  }
  var prodName = [];
   for(var i=0;i<allHref .length;i++){
  prodName.push(allHref [i].slice(0, -1))
  }
  
  var prodNameNew=[];
  for(var i=0;i<prodName .length;i++){
  prodNameNew.push(prodName[i].slice(0, -1))
  }

So, the final result is on the attached screen. N

How i have concatenate all the elements of each array in order to get new arrays in the following format: Nokia G20 4 64 Albastru, Motorola G60 gri etc Thank You

enter image description here

Olga
  • 41
  • 5
  • 3
    please post the input data, your code, and the expected output ... a picture of input data is useless, your code is absent, and a vague description of the expected output is less than useless – Bravo Mar 01 '22 at 08:28
  • 3
    Assuming you're after string elements, you can use [`.join()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) as you push – Nick Parsons Mar 01 '22 at 08:28
  • assuming you have a 2d array and want a 1d array ... array has a `flat` method ... easy - or you can `newStr1.push(...newStr[i])` in your picture of code – Bravo Mar 01 '22 at 08:29
  • @Olga I added an answer. Did you get a chance to look into that. Hope it will work as per your expectation. – Debug Diva Mar 01 '22 at 14:02

2 Answers2

0

You want to capitalize the items of the inner arrays and then join them by space.

One way to do it is:

let result = arr.map(a => a.map(capitalize))
  .map(a => a.join(" "))

where capitalize should be a function that takes a string and returns a string with first letter in upper case if possible. You can find this in answers for the more specific SO question: How do I make the first letter of a string uppercase in JavaScript?

B0Andrew
  • 1,725
  • 13
  • 19
0

Instead of 3 separate loops, we can get the substring based on single iteration only using Array.map() along with String.slice().

const prodHref = [{
  href: "https://abc/def/ghi/alpha/mno"
}, {
  href: "https://abc1/def1/ghi1/beta/mno1"
}, {
  href: "https://abc2/def2/ghi2/gamma/mno2"
}, {
  href: "https://abc3/def3/ghi3/omicron/mno3"
}];

const allHref = prodHref.map((obj) => obj.href.split('/')[5].slice(0, -2));

console.log(allHref);

Now you can use Array.join() method to join the result array.

const allHref = ["alp", "be", "gam", "omicr"];

console.log(allHref.join(" "));
Debug Diva
  • 26,058
  • 13
  • 70
  • 123