0

I'm wanting to append something to a JS Object similar to how you can push to an array. I know that you can't push to an object. So I'm trying to find another solution. It's a funky workaround to accomplish pagination.

$scope.showMore = function() {
    $rootScope.resultsObject = parseResults($rootScope.keywordData, $rootScope.limit);
}

That's the line of code where I'm running into problems.

parseResults() is taking my keywordData, and transforming it into columns of giant strings. show more

When showMore() is called, it is being assigned over resultsObject, thereby removing the first 2 rows of data. I'd rather have it add 2 more rows to the end.

Previous attempts at solving this involved keeping a running array of what was in resultsObject, then I would push more results into that array & try to rerun it through parseResults. This didn't work. It would create additional columns, or dump all the data into one single column.

So now I turn to the JS experts for a better solution.

I've tried working a fiddle up, but it doesn't function correctly. I do have a JSFiddle if you wish to see more of the code. Just remember that it doesn't work.

UPDATE:

Maybe my problem is with my parseResults(). Let me show you what the object actually looks like after being parsed:

resultsObject{
    column0: "column1<br>test1"
    column1: "column2<br>test2-1"
    column2: "column3<br>test3"
    column3: "column4<br>test4"
    column4: "column5<br>test5"
    column5: ""
}

So when I append to resultsObject, I want it to add onto the strings for each value. How is this possible?

1 Answer 1

1

The angular extend method should allow you to do this.

var objectAB = { a: 1, b: 2 },
    objectCD = { c: 3, d: 4 };

var objectABCD = angular.extend(objectAB, objectCD);

console.log(objectABCD);
// { a: 1, b: 2, c: 3, d: 4 }
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not aware of the extend method. I like the look of it. However, it didn't function as I would expect. $rootScope.resultsObject = angular.extend($rootScope.resultsObject, parseResults($rootScope.keywordData, $rootScope.limit)); I believe this is the right syntax. But it just gives me exactly what I had before.
It hard to say what the problem is, your fiddle example does not seem to be working. You should try to console.log() the values of your input params. This may give you some insight as to why it does not return the results you expect.
Think I may have realized my problem. Within ParseResults, I'm assigning keys as Column[i]... And it's starting with the same index... I'm overwriting things there! grrrrr
I've added an update. Does that help add additional clarification?

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.