---
title: "how to push data in array if data in another array?"  
description: "how to push data in array if data in another array?"  
author: "Anonymous User"  
published: 2014-10-31  
updated: 2014-10-31  
canonical: https://www.mindstick.com/forum/2474/how-to-push-data-in-array-if-data-in-another-array  
category: "jquery"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# how to push data in array if data in another array?

data in this array

\

```
var tDataValues = {        id: "Userid",        text: "Name",        username: "Username",        cnic: 'CNIC'    }
```

I am sending this [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) to the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server)

commonSelect2Templating([selector](https://www.mindstick.com/interview/23419/action-selector-in-mvc), url, tDataValues, minInputLength, [placeholder](https://www.mindstick.com/forum/1519/how-do-i-remove-a-listbox-new-item-placeholder));

Note: I'm using Jquery Select2 (Sharing if it can help my [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) to understand.)

Then in that function in results Section i [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to assign values

```
results: function(data, page) {        var newData = [];        var length = data.length + 1;        for(var i = 0; i<=length; i++){         }        $.each(data, function (index,value) {            newData.push({                id: value[tDataValues.id],                text: value[tDataValues.text]            });        });        return { results: newData };    }
```

This is the data coming from the Server:\
\

```
[{ " Userid ": "21", "Name": "Rohit Kesharwani", "Username": "Rohit", "CNIC": "16141-6321136-1" }, { " Userid ": "22", "Name": "Kamlakar Singh", "Username": "Kamlakar", "CNIC": "17301-5856870-1" }, { " Userid ": "23", "Name": "Pawan Shukla", "Username": "Pawan", "CNIC": "15165-6156685-6" }]
```

**Coming to the The Problem:** [Right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now) all the magic is happening here.\
\

```
$.each(data, function (index, value) {        newData.push({            id: value[tDataValues.id],            text: value[tDataValues.text]        });    });
```

Its Telling the code which is id and which is text, and it is working perfectly fine.

Now the Problem here is function i am trying to make is Common Function for the select2, and if i have more values from db, like now i [am getting](https://www.mindstick.com/forum/34179/i-am-getting-error-while-assigning-the-data-to-the-array-list) from [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) how to make a loop and set those values one by one to its proper context. e-g tDataValues holds the [fieldName](https://www.mindstick.com/forum/34722/how-to-get-label-text-from-table-reference-on-database-based-on-tablename-and-fieldname) cnic and server is sending the fieldName CNIC so how to make a loop that if cnic exist in both then it should simply assign,

i can do that manually

```
newData.push({        cnic: value[tDataValues.cnic]    });
```

But like this it can not be a common function.

\

## Replies

### Reply by Mark M

Try:

```
for (var key in tDataValues) {        newData.push({ key:
value[tDataValues[key]] });    }
```

## Update:

then create an object first and push it to the array:

```
for (var key in tDataValues) {        var obj = {};        obj[key] = value[tDataValues[key]];        newData.push(obj);    }
```


---

Original Source: https://www.mindstick.com/forum/2474/how-to-push-data-in-array-if-data-in-another-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
