如何使用新属性展平对象下划线?

时间:2016-07-29 03:54:53

标签: javascript arrays underscore.js

我有一个这样的阵列:

var arr = [
   {
    name: 'John',
    age: {
        id: 1,
        value: 'less than 19'
      }
   },
   {
    name: 'Doe',
    age: {
         id: 2,
         value: 'more than 19'
      }
   }
]

如何使用下划线来展平数组中的age对象。预期结果是:

arr == [
       {
        name: 'John',
        age: 'less than 19'
       },
       {
        name: 'Doe',
        age: 'more than 19'
       }
    ];

谢谢,

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

var arr = [{
  name: 'John',
  age: {
    id: 1,
    value: 'less than 19'
  }
}, {
  name: 'Doe',
  age: {
    id: 2,
    value: 'more than 19'
  }
}];

var result = arr.map(function(item) {
  return {
    name: item.name,
    age: item.age.value
  };
});

console.log(result);

<强>演示:

Set rngDataHeadings = Range("B11", "T11")

我希望这会对你有所帮助。

答案 1 :(得分:0)

使用旧式:D

window.onload = function() {                

            document.getElementById('submit').addEventListener('click', validateInput);
        }

        var userPass = document.getElementById('password').value;
        var confirmPass = document.getElementById('confirm_password').value;

        function validatePassword() {
            var userPass = document.getElementById('password').value;

            if (userPass.length == 0) { // Nothing was entered
                document.getElementById('password_error').innerHTML = 'Please enter a password';
            } else if (userPass.length < 6) { // Less than 6 chars 
                document.getElementById('password_error').innerHTML = 'Your password must be at least 6 characters in length'; 
            } else if (userPass.match(/\s/)) { // contains a space
                document.getElementById('password_error').innerHTML = 'Your password cannot contain spaces';
            } else if (!userPass.match(/\d/)) { // Does not contain a number
                document.getElementById('password_error').innerHTML = 'Your password must contain a number';
            } else if (userPass !== confirmPass) {
                document.getElementById('confirm_error').innerHTML = 'Passwords do not match. Please check again.';             
            } else {
                document.getElementById('password_error').innerHTML = "";
                document.getElementById('confirm_error').innerHTML = "";
                alert("Success!");  
                clearFields();
            }
        }

        function validateConfirm() {

            function clearFields (){
                document.getElementById('password').value=null;
                document.getElementById('confirm_password').value=null;
            }

            if (confirmPass.length == 0) {
                document.getElementById('confirm_error').innerHTML = "Please confirm password to proceed";
            } else if (confirmPass.length < 6) {
                document.getElementById('confirm_error').innerHTML = "Your password must be at least 6 characters in length";
            } else if (userPass !== confirmPass) {
                document.getElementById('confirm_error').innerHTML = 'Passwords do not match. Please check again.';     
            } else {
                document.getElementById('password_error').innerHTML = "";
                document.getElementById('confirm_error').innerHTML = "";
                alert("Success!");  
                clearFields();
            }              
        }  


        function validateInput() {
            validatePassword();
            validateConfirm();
        }