Home > Mobile >  trying to use copy(JSON.stringify(result))
trying to use copy(JSON.stringify(result))

Time:12-18

I am using the console in my browser to scraper and sort content using JS. Here is my code

This is my result array

var arr = [
"George\nPresident & Founder",

"content",

 "Ronald\nCountry Director, America",

 "content",

 "Kriss, PhD\nVice President",

 "content",

 "Joseph, MS\nDirector",

 "content",

 "Elizabeth\nDevelopment Operations",

 "content",

 "Lisa, MFA, MBA\nU.S. Content",

 "content.",

 "Natalia\nCountry Director"
]

Here is what I have tried:

  for(var i=0; len = result.length, i < len; i  ){
    result[i]['something'] = [];
    if(i === 0){
        result[i].split('\n');
    }
    else if (i % 2 === 0) {
        result[i].split('\n');
    }
    console.log(result[i]);
    result[i]['test'].push(result[i]);

  }

This comes back as result[i]['something'] = []; is undefined but when i console.log(result[i]) i get the right result. Ive tried to copy(JSON.stringify(result[i])) but I only get back one object.

for(var i=0; len = result.length, i < len; i  ){
    var arr = [];
    if(i === 0){
        result[i].split('\n');
    }
    else if (i % 2 === 0) {
        result[i].split('\n')
    }
    arr.push(result[i])
    // console.log(result[i]);
    console.log(arr);
  }

This doesnt split them, it just pushes them into the arrays.

When I console.log(result[i]) I get: (this is right but its not in strings nor is it in arrays; I also cant copy this either)

George
President & Founder

 content 

  Ronald
  Country Director America 

  content 

  Kriss PhD
  Vice President 

  content 

  Joseph MS
  Director 

  content 

  Elizabeth
  Development Operations 

  content 

  Lisa MFA MBA
  U.S. Content 

  content

  Natalia
  Country Director 

My end goal should look like this:

var result = [
["George"],
["President & Founder"],

[ "content" ],

[ "Ronald"]
["Country Director, America" ],

[ "content" ],

[ "Kriss, PhD"],
["Vice President" ],

[ "content" ],

[ "Joseph, MS"],
["Director" ],

[ "content" ],

[ "Elizabeth"],
["Development Operations" ],

[ "content" ],

[ "Lisa, MFA, MBA"],
["U.S. Content" ],

[ "content." ],

[ "Natalia"],
["Country Director" ],
[ "content." ]
]

What can I do so that I get the result[i] and copy it onto my clipboard using copy(JSON.stringify(result))?

CodePudding user response:

I would generate an array of objects. Since you have two rows in the array per person (except for Natalia, she has no content?), you can loop through the array jumping two at a time gathering the info for each person into an object.

var arr = [
"George\nPresident & Founder",

"content",

 "Ronald\nCountry Director, America",

 "content",

 "Kriss, PhD\nVice President",

 "content",

 "Joseph, MS\nDirector",

 "content",

 "Elizabeth\nDevelopment Operations",

 "content",

 "Lisa, MFA, MBA\nU.S. Content",

 "content.",

 "Natalia\nCountry Director"
];

const result = [];
for (let i=0; i < arr.length; i =2) {
  const nameParts = arr[i].split("\n");
  result.push({
    name: nameParts[0],
    title: nameParts[1],
    content: arr[i   1]
  });
}

console.log(result);

CodePudding user response:

The other answers seem intriguing. I will suggest a slightly longer one which has a few for loops.

This is what I came up with:

UPDATED: I've updated it to work now that you have updated your post as well.

function changeArr(array) {
    // create a temporary array
    let tempArr = [];
    
    // loop through the array which was passed as an argument
    for (let i = 0; i < array.length; i  ) {
        let splitArr = array[i].split("\n");

        // splitArr returns an array containing the 2 strings which have been split

        // using the spread operator from ES6 we can spread out the elements and push them into tempArr
        tempArr.push(...splitArr);
    }
    
    // tempArr should now contain your end result
    return tempArr;
}

How to use:

changeArr(arr);

/*
this will return:

=> [
    "George",
    "President & Founder",

    "content",

    "Ronald",
    "Country Director, America",

    "content",

    "Kriss, PhD",
    "Vice President",

    "content",

    "Joseph, MS",
    "Director",

    "content",

    "Elizabeth",
    "Development Operations",

    "content",

    "Lisa, MFA, MBA",
    "U.S. Content",

    "content",

    "Natalia",
    "Country Director",

    "content"
]

With this, you can either make another variable called let result and store whatever the function returns in it.

let result = changeArr(arr);

Or you can assign whatever the function returns to the arr variable.

arr = changeArr(arr);

CodePudding user response:

remove arr.push(result[i]) and try this

  if (i % 2 == 0) {
      arr.push( result[i].split('\n'));
    }
else
{
   arr.push(["content"]);
}

CodePudding user response:

You can acheive your goal by using reduce method much more easier like this:

let arr = [
  "George\nPresident & Founder",
  "content",
  "Ronald\nCountry Director, America",
  "content",
  "Kriss, PhD\nVice President",
  "content",
  "Joseph, MS\nDirector",
  "content",
  "Elizabeth\nDevelopment Operations",
  "content",
  "Lisa, MFA, MBA\nU.S. Content",
  "content.",
  "Natalia\nCountry Director"
]
let result = arr.reduce((acc, str) => {
  str.split('\n').forEach(str => acc.push([str]));
  return acc;
}, [])
console.log(result)

  • Related