0

I have a variable list of elements and need to get their names and values in pairs (associative array or object).

So far I have the following but how do I create the pairs of name and value for each element ?

var names = [];
var values = [];

$('.modalField').each(function() {
    fieldName = $(this).attr('name');
    fieldVal = $(this).val();
    
    names.push(fieldName);
    values.push(fieldVal);
});

Thanks for any help,
Tom

keewee279
  • 1,656
  • 5
  • 28
  • 60
  • What's wrong with your current code? You can just use `name[i]` and `values[i]` to get the name and value for any particular field... – Nick Aug 14 '20 at 13:08
  • Does this answer your question? [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Heretic Monkey Aug 14 '20 at 13:11
  • Take a look at this post this should help you https://stackoverflow.com/questions/19787868/create-an-associative-array-in-jquery – Sahasrar Aug 14 '20 at 13:11

4 Answers4

1

Use bracket notation.

var assoc = {};
$('.modalField').each(function() {
    let fieldName = $(this).attr('name');
    let fieldVal = $(this).val();
    assoc[fieldName] = fieldVal;
});

(Also, you should initialize your variables with let/var/const inside the function so they don't leak into the global scope.)

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
0

You say name-value but you seem to want an object

You can do EITHER

// Object
const obj = {};
$('.modalField').each(function() {
  const fieldName = $(this).attr('name');
  const fieldVal = $(this).val();
  obj[fieldName] = fieldVal
});

console.log(obj)

// OR Name-Value pair


const arr = $('.modalField').map(function() {
  return {
    [$(this).attr('name')]: $(this).val()
  }
}).get()


console.log(arr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="a" value="1" class="modalField" />
<input name="b" value="2" class="modalField" />
<input name="c" value="3" class="modalField" />
<input name="d" value="4" class="modalField" />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

Something like this?

var fields = [];

$('.modalField').each(function() {
    var name = $(this).attr('name');
    var value = $(this).val();
    
    fields.push({ name, value });
});

console.log(fields);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input class="modalField" name="n1" value="v1" />
  <input class="modalField" name="n2" value="v2" />
  <input class="modalField" name="n3" value="v3" />
</div>

Output:

[
  {
    "name": "n1",
    "value": "v1"
  },
  {
    "name": "n2",
    "value": "v2"
  },
  {
    "name": "n3",
    "value": "v3"
  }
]
Shadab
  • 1,297
  • 6
  • 9
0

Instead of having name & values array, I would suggest you to create an object.

var obj ={};
$('.modalField').each(function() {
    obj[$(this).attr('name')] = $(this).val();
   
});

So this way you will have your object like this (names used as example only): {"name1":"val1","name2":"val2","name3":"val3"}