skip to Main Content

How i can assign the nc key value to nc variable which i have declared.

const s = [
'nc: https://nector.com',
'tw: https://twitter.com; li: https://www.linkedin.com; fb: https://www.facebook.com'
]

var li = '';
var tw = '';
var fb = '';
var nc = "";

s.forEach((items) => {
const a = items.split(" ")
const search = query => a.filter(str => str.includes(query))


li = search("linkedin")
tw = search("twitter") 
fb = search("facebook")

})

})

I have tried doing it with matching the char in the string able to do it but looking for solution so it can be done on the basis of key.

Note: Array elements are little weird.

3

Answers


  1. This code extracts all key-value pairs as an array, then constructs an object from the result. Finally, we destructure to extract object properties into variables.

    const s = [
    'nc: https://nector.com',
    'tw: https://twitter.com; li: https://www.linkedin.com; fb: https://www.facebook.com'
    ]
    
    const {li, tw, fb, nc} = Object.fromEntries(
      s.join('; ').split('; ').map(i=>i.split(': '))
    )
    
    console.log(li, tw, fb, nc);
    Login or Signup to reply.
  2. You can use reduce method to do this. The steps would be the following-

    1. iterate over each item in the s array using reduce and spit the item by ; .
    2. Again iterate over each element and split it by : and assign it into a key-value pair.
    3. And finally create an object of these key-value pairs.

    Here is the demo-

    const s = [
      'nc: https://nector.com',
      'tw: https://twitter.com; li: https://www.linkedin.com; fb: https://www.facebook.com'
    ];
    
    const { nc, li, tw, fb } = s.reduce((r, item) => {
      const kvPairs = item.split('; ');
      kvPairs.forEach((kv) => {
        const [key, value] = kv.split(': ');
        r[key] = value;
      });
      return r;
    }, {});
    
    console.log(nc, li, tw, fb);
    Login or Signup to reply.
  3. You can modify your code to assign the nc key value to the nc variable like this:

    const s = [
      'nc: https://nector.com',
      'tw: https://twitter.com; li: https://www.linkedin.com; fb: https://www.facebook.com',
    ];
    
    var li = '';
    var tw = '';
    var fb = '';
    var nc = '';
    
    s.forEach((items) => {
      const a = items.split(' ');
    
      const search = (query) => a.filter((str) => str.includes(query));
    
      li = li || search('li:');
      tw = tw || search('tw:');
      fb = fb || search('fb:');
      nc = nc || search('nc:');
    });
    
    // Remove the prefixes and keep only the URLs
    li = li.length > 0 ? li[0].replace('li:', '') : '';
    tw = tw.length > 0 ? tw[0].replace('tw:', '') : '';
    fb = fb.length > 0 ? fb[0].replace('fb:', '') : '';
    nc = nc.length > 0 ? nc[0].replace('nc:', '') : '';
    
    console.log(li);
    console.log(tw);
    console.log(fb);
    console.log(nc);

    Here’s what has been changed:

    Updated the search query for each variable to include the colon (e.g., li: instead of linkedin).
    After the forEach loop, added code to remove the prefixes and keep only the URLs for each variable.
    Now the nc variable will be assigned the value corresponding to the nc key from the input array s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search