0

Im looking for a way to combine the two objects into a single function or something that would lessing my line count.

var fsInfo1 = {
    name: 'Joshua Velez',
    address:{
        street: '94-1411 Kulewa Loop ',
        city: 'Waipahu, ',
        state: 'HI'
    },
    gpa: [3.3,4.0,2.8]

}
var fsInfo2= {
    name: 'John Doe',
    address:{
        street: 'Some Street ',
        city: 'Wahiawa, ',
        state: 'HI'
    },
    gpa: [3.4,3.6,3.1]
}

console.log("Name: " + fsInfo1.name)

console.log("Address: " + fsInfo1.address['street'] + fsInfo1.address['city'] + fsInfo1.address['state']) console.log("GPA: " + fsInfo1.gpa[0],fsInfo1.gpa[1],fsInfo1.gpa[2])

console.log("Name: " + fsInfo2.name)

console.log("Address: " + fsInfo2.address['street'] + fsInfo2.address['city'] + > fsInfo2.address['state']) console.log("GPA: " + fsInfo2.gpa[0],fsInfo2.gpa[1],fsInfo2.gpa[2])

6
  • 1
    An array will only save you the fsInfo2= string: var arr = [{ name: 'Joshua Velez', address:{ street: '94-1411 Kulewa Loop ', city: 'Waipahu, ', state: 'HI' }, gpa: [3.3,4.0,2.8] }, { name: 'John Doe', address:{ street: 'Some Street ', city: 'Wahiawa, ', state: 'HI' }, gpa: [3.4,3.6,3.1] }] Commented Oct 12, 2013 at 6:00
  • Which linecount are you trying to reduce? Your objects are still going to contain the data. You can create a loop to go through them if you make them into an array Commented Oct 12, 2013 at 6:02
  • no specific line count just think its to much code which can be simplified Commented Oct 12, 2013 at 6:04
  • And perhaps more to the point, WHY are you trying to reduce line count? There might be some valid reasons to refactor this code, (for example, you might define a function to do the output), but "fewer lines" shouldn't be one of them. Commented Oct 12, 2013 at 6:04
  • Yes I need later on I need to add another person with same info with this function addData('John Doe', '42 Walibi Way', 'Sydney', 'AU', [3.4,3.0,3.7]) Commented Oct 12, 2013 at 6:09

2 Answers 2

1

One approach is:

var fsInfo1 = // same as before
var fsInfo2 = // same 
function logFsInfo(fsInfo) {
    console.log("Name "+fsInfo.name);
    console.log("Address "+fsInfo.address.street + fsInfo.address.city + fsInfo.address.state);
    console.log("GPA "+fsInfo.gpa.join(",")); //combine gpa values into a string separated by commas
}
logFsInfo(fsInfo1);
logFsInfo(fsInfo2);

But it really depends on what you want to do here. Is all you're doing printing out fsInfo data? If so, sure, just do something like this as a shortcut to printing out that data. But if you want to start doing other things with fsInfo objects, I'd suggest making a class and attaching methods to it. But, that would be complete overkill for a simple use.

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

Comments

0
var fsInfo1 = {
    name: 'Joshua Velez',
    address:{
        street: '94-1411 Kulewa Loop ',
        city: 'Waipahu, ',
        state: 'HI'
    },
    gpa: [3.3,4.0,2.8]

}
var fsInfo2= {
    name: 'John Doe',
    address:{
        street: 'Some Street ',
        city: 'Wahiawa, ',
        state: 'HI'
    },
    gpa: [3.4,3.6,3.1]
}
var obj = new Object();

obj[0] = fsInfo1;
obj[1] = fsInfo2;
var count = Object.keys(myArray).length;
for(i=0 ; i<count;i++)
{
      console.log("Name: " + obj[i].name)

console.log("Address: " + obj[i].address['street'] + obj[i].address['city'] + obj[i].address['state']) console.log("GPA: " + obj[i].gpa[0],obj[i].gpa[1],obj[i].gpa[2])
}

1 Comment

In which part you want explanation?

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.