JavaScript find属性是重复属性名称的嵌套对象
我有一个函数,它通过名称在嵌套对象中查找属性并返回其值。 我从这个网站上的另一个问题得到的功能,到目前为止,它一直工作得很好。 我看起来像这样:JavaScript find属性是重复属性名称的嵌套对象,javascript,Javascript,我有一个函数,它通过名称在嵌套对象中查找属性并返回其值。 我从这个网站上的另一个问题得到的功能,到目前为止,它一直工作得很好。 我看起来像这样: var _getPropertyValue = function (obj, field) { // Create a result var result = null; // If our object is an array if (obj instanceof Array) { // For
var _getPropertyValue = function (obj, field) {
// Create a result
var result = null;
// If our object is an array
if (obj instanceof Array) {
// For each array item
for (var i = 0; i < obj.length; i++) {
// Invoke our function for the current object
result = _getPropertyValue(obj[i], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
// If we are an object
} else {
// For each property in our object
for (var prop in obj) {
// If our property matches our value
if (prop == field) {
// Return our value
return obj[prop];
}
// If our property is an object or an array
if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
// Invoke our function for the current object
result = _getPropertyValue(obj[prop], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
}
}
// Return our result
return result;
};
{
name: 'test',
mode: 'default',
settings: {
mode: 'auto'
}
}
settings.mode
// Private function to get the value of the property
var _getPropertyValue = function (object, notation) {
// Get all the properties
var properties = notation.split('.');
// If we only have one property
if (properties.length === 1) {
// Return our value
return object[properties];
}
// Loop through our properties
for (var property in object) {
// Make sure we are a property
if (object.hasOwnProperty(property)) {
// If we our property name is the same as our first property
if (property === properties[0]) {
// Remove the first item from our properties
properties.splice(0, 1);
// Create our new dot notation
var dotNotation = properties.join('.');
// Find the value of the new dot notation
return _getPropertyValue(object[property], dotNotation);
}
}
}
};
// Private function to get the value of the property
var _getPropertyValue = function (obj, notation) {
// Get our properties
var properties = notation.split('.');
// Use reduce to get the value of the property
return properties.reduce(function (a, b) {
// Return our value
return a[b];
}, obj);
};
使用我的函数,我相信它将找到第一个模式,然后退出该函数。
我希望这样指定字段参数:
var _getPropertyValue = function (obj, field) {
// Create a result
var result = null;
// If our object is an array
if (obj instanceof Array) {
// For each array item
for (var i = 0; i < obj.length; i++) {
// Invoke our function for the current object
result = _getPropertyValue(obj[i], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
// If we are an object
} else {
// For each property in our object
for (var prop in obj) {
// If our property matches our value
if (prop == field) {
// Return our value
return obj[prop];
}
// If our property is an object or an array
if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
// Invoke our function for the current object
result = _getPropertyValue(obj[prop], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
}
}
// Return our result
return result;
};
{
name: 'test',
mode: 'default',
settings: {
mode: 'auto'
}
}
settings.mode
// Private function to get the value of the property
var _getPropertyValue = function (object, notation) {
// Get all the properties
var properties = notation.split('.');
// If we only have one property
if (properties.length === 1) {
// Return our value
return object[properties];
}
// Loop through our properties
for (var property in object) {
// Make sure we are a property
if (object.hasOwnProperty(property)) {
// If we our property name is the same as our first property
if (property === properties[0]) {
// Remove the first item from our properties
properties.splice(0, 1);
// Create our new dot notation
var dotNotation = properties.join('.');
// Find the value of the new dot notation
return _getPropertyValue(object[property], dotNotation);
}
}
}
};
// Private function to get the value of the property
var _getPropertyValue = function (obj, notation) {
// Get our properties
var properties = notation.split('.');
// Use reduce to get the value of the property
return properties.reduce(function (a, b) {
// Return our value
return a[b];
}, obj);
};
让它直接到达我的对象中的那个位置,并返回值。
有人知道能不能做到吗
这家店正在关门。 我自己这样回答:
var _getPropertyValue = function (obj, field) {
// Create a result
var result = null;
// If our object is an array
if (obj instanceof Array) {
// For each array item
for (var i = 0; i < obj.length; i++) {
// Invoke our function for the current object
result = _getPropertyValue(obj[i], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
// If we are an object
} else {
// For each property in our object
for (var prop in obj) {
// If our property matches our value
if (prop == field) {
// Return our value
return obj[prop];
}
// If our property is an object or an array
if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
// Invoke our function for the current object
result = _getPropertyValue(obj[prop], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
}
}
// Return our result
return result;
};
{
name: 'test',
mode: 'default',
settings: {
mode: 'auto'
}
}
settings.mode
// Private function to get the value of the property
var _getPropertyValue = function (object, notation) {
// Get all the properties
var properties = notation.split('.');
// If we only have one property
if (properties.length === 1) {
// Return our value
return object[properties];
}
// Loop through our properties
for (var property in object) {
// Make sure we are a property
if (object.hasOwnProperty(property)) {
// If we our property name is the same as our first property
if (property === properties[0]) {
// Remove the first item from our properties
properties.splice(0, 1);
// Create our new dot notation
var dotNotation = properties.join('.');
// Find the value of the new dot notation
return _getPropertyValue(object[property], dotNotation);
}
}
}
};
// Private function to get the value of the property
var _getPropertyValue = function (obj, notation) {
// Get our properties
var properties = notation.split('.');
// Use reduce to get the value of the property
return properties.reduce(function (a, b) {
// Return our value
return a[b];
}, obj);
};
我进一步改进了这一点,如下所示:
var _getPropertyValue = function (obj, field) {
// Create a result
var result = null;
// If our object is an array
if (obj instanceof Array) {
// For each array item
for (var i = 0; i < obj.length; i++) {
// Invoke our function for the current object
result = _getPropertyValue(obj[i], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
// If we are an object
} else {
// For each property in our object
for (var prop in obj) {
// If our property matches our value
if (prop == field) {
// Return our value
return obj[prop];
}
// If our property is an object or an array
if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
// Invoke our function for the current object
result = _getPropertyValue(obj[prop], field);
// If we have a result
if (result) {
// Exit the loop
break;
}
}
}
}
// Return our result
return result;
};
{
name: 'test',
mode: 'default',
settings: {
mode: 'auto'
}
}
settings.mode
// Private function to get the value of the property
var _getPropertyValue = function (object, notation) {
// Get all the properties
var properties = notation.split('.');
// If we only have one property
if (properties.length === 1) {
// Return our value
return object[properties];
}
// Loop through our properties
for (var property in object) {
// Make sure we are a property
if (object.hasOwnProperty(property)) {
// If we our property name is the same as our first property
if (property === properties[0]) {
// Remove the first item from our properties
properties.splice(0, 1);
// Create our new dot notation
var dotNotation = properties.join('.');
// Find the value of the new dot notation
return _getPropertyValue(object[property], dotNotation);
}
}
}
};
// Private function to get the value of the property
var _getPropertyValue = function (obj, notation) {
// Get our properties
var properties = notation.split('.');
// Use reduce to get the value of the property
return properties.reduce(function (a, b) {
// Return our value
return a[b];
}, obj);
};
那不行。正在从文本框填充该字段。