2

I have some raw imdb data in two strings:

var headers = "ID   imdbID  Title   Year    Rating  Runtime Genre   Released    Director    Writer  Cast    Metacritic  imdbRating  imdbVotes   Poster  Plot    FullPlot    Language    Country Awards  lastUpdated";


const content = "1  tt0000001   Carmencita  1894    NOT RATED   1 min   Documentary, Short      William K.L. Dickson        Carmencita      5.8 1136    http://ia.media-imdb.com/images/M/MV5BMjAzNDEwMzk3OV5BMl5BanBnXkFtZTcwOTk4OTM5Ng@@._V1_SX300.jpg    Performing on what looks like a small wooden stage, wearing a dress with a hoop skirt and white high-heeled pumps, Carmencita does a dance with kicks and twirls, a smile always on her face.           USA     2016-05-04 00:03:31.600000000";

Of course I am cleaning out the tabs and spaces etc and creating two arrays like so:

const values = content.split(/(\t+)/).filter( (part) => !/\t/.test(part) );

let keys = headers.split(/(\s+)/).filter( (part) => !/\s+/.test(part) );

Now I want to map the keys in keys array to the values in the values array accurately. So I do a reduce like so:

var result = {};

values.reduce( (acc, val, index) => {
  return result[ keys[index] ] = val; 
}, result);

This gives me back a final result object that looks like this:

 {
  Cast: "1136",
  Director: "Carmencita",
  Genre: "Documentary, Short",
  ID: "1",
  imdbID: "tt0000001",
  imdbRating: "Performing on what looks like a small wooden stage, wearing a dress with a hoop skirt and white high-heeled pumps, Carmencita does a dance with kicks and twirls, a smile always on her face.",
  imdbVotes: "USA",
  Metacritic: "http://ia.media-imdb.com/images/M/MV5BMjAzNDEwMzk3OV5BMl5BanBnXkFtZTcwOTk4OTM5Ng@@._V1_SX300.jpg",
  Poster: "2016-05-04 00:03:31.600000000",
  Rating: "NOT RATED",
  Released: "William K.L. Dickson",
  Runtime: "1 min",
  Title: "Carmencita",
  Writer: "5.8",
  Year: "1894"
}

As you can see, the order is all mixed up! How do I get the right order in my data so I get title key mapped to the movie title value etc. ?

I am open to using a library like lodash to achieve this but can't tell what I should use? Here is a jsbin demo of the present state: https://jsbin.com/gekawi/edit?js,console

5
  • is the data separated by tab? if so, please include it into the question. Commented Jun 8, 2016 at 9:32
  • Yes - but the tabs are cleaned out Commented Jun 8, 2016 at 9:32
  • no, please supply the raw data. Commented Jun 8, 2016 at 9:33
  • raw data is in the first code snapshot and also in this jsbin: jsbin.com/gekawi/edit?js,console Commented Jun 8, 2016 at 9:34
  • I updated the question for accuracy @NinaScholz - had mistakenly stated arrays where I showed strings Commented Jun 8, 2016 at 9:35

1 Answer 1

2

If you take the raw data with the tabs inside, you could use them for splitting.

var headers = "ID	imdbID	Title	Year	Rating	Runtime	Genre	Released	Director	Writer	Cast	Metacritic	imdbRating	imdbVotes	Poster	Plot	FullPlot	Language	Country	Awards	lastUpdated",
    content = "1	tt0000001	Carmencita	1894	NOT RATED	1 min	Documentary, Short		William K.L. Dickson		Carmencita		5.8	1136	http://ia.media-imdb.com/images/M/MV5BMjAzNDEwMzk3OV5BMl5BanBnXkFtZTcwOTk4OTM5Ng@@._V1_SX300.jpg	Performing on what looks like a small wooden stage, wearing a dress with a hoop skirt and white high-heeled pumps, Carmencita does a dance with kicks and twirls, a smile always on her face.			USA		2016-05-04 00:03:31.600000000",
    headerArray = headers.split(/\t/),
    contentArray = content.split(/\t/),
    object = {};

headerArray.forEach(function (k, i) {
    object[k] = contentArray[i];
});

console.log(object);

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Could you tell me why it didn't work when I split the data with spaces?
because you have some tabs together and you can not decide later which is what slot to assign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.